Selecting block of code in Visual Studio Code

Viewed 36878

Is there a keyboard shortcut or an extension that would allow me to select a block of code?

I'd like to select everything between curly braces, between HTML tags, etc.

8 Answers

Use Alt + Shift + → to expand the selection between braces or tags.

Use Alt + Shift + ← to shrink the selection between braces or tags.

Here is the Microsoft Visual C++ shortcuts cheatsheet that might help you.

Update 2019/3: this inner functionality of Visual Studio is not working very well after some updates. Alt + Shift + → now selects things including braces (which annoys me).

It is not like its behavior before. It selected things between curly braces, when I first posted this answer. I'm using Mark's answer now.

If anyone has a better solution (without an extension) now, please leave a comment.

On Mac Ctrl + Shift + → to expand the selection. Press multiple times to expand to the block.

A quicker way is selecting a line then expanding the selection like this:

  1. Select line
    Ctrl + L
  2. Expand selection
    Alt + Shift + →

Doing this inside a block (HTML element, JavaScript curly braces) will select the inner block (HTML element content, inside curly braces). Do step 2 again to select block including the container (HTML element, whole function, class, etc.)

A real working solution:

Press Command + P and search for Select to Bracket Command P Select to Bracket

To bind it to a key, press the little Settings icon on the right. The "Keyboard Shortcuts" Window will appear as shown in the image. Double-click on Select to Bracket and press a Keyboard shortcut you like, for example Command + Shift + H. Keyboard Shortcuts Select to Bracket

Now, whenever you want to select code in a block, put your cursor inside the block and press your shortcut. It is like magic. Example of Select to Bracket

In Visual Studio Code, there is a new option called Balance. First you can place the cursor in a suitable block. After that, you can press Ctrl + Shift + P. Type balance and it lists like below.

Enter image description here

Now press Enter, it will select the related code block like below. For simple use, you can add a shortcut key binding.

Enter image description here

A real working solution:

Search for Select to Bracket and bind it to whatever keys you like.

It is like magic.

enter image description here

If you are using Java in Visual Studio Code and you don't want your block selection to include the brackets (or any other peripheral character) then do the following:

  • Go to Visual Studio Code settings by pressing Ctrl + ,.
  • Search for "Java selection range" and deselect it.
Related