70

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.

3
  • Are these the best answers for this question in 2019?
    – Mario
    Commented Sep 8, 2019 at 1:08
  • @Mario You're right. I've changed the accepted answer to one that uses native feature.
    – Devin
    Commented Jun 29, 2021 at 7:59
  • Note that this question can be somewhat confusing because block selection, which has long been a feature of many editors is a way to select a rectangle of characters. This question shows up when you search for block selection in VS Code. You can do that with Alt+Shift+Arrows in VS or Ctrl+Alt+Shift+Arrows in VS Code. This question is about selecting blocks of code in the sense of scopes like literals, braced blocks, functions, classes, etc. which VS allows to select with Alt+Shift+Left/Right Arrows. Commented Jun 13, 2023 at 18:40

9 Answers 9

82

For Windows/Linux

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.

1
  • 1
    Use alt + shift + → to expand selection between braces or tags. Then, while keep holding alt + shift. click → again several times to append test from the left side of the braces until the function name as part of the same selection. Enjoy :)
    – Omtechguy
    Commented Nov 13, 2019 at 17:21
40

For MacOS

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

3
  • 1
    "Press multiple times to expand to the block." is the key :) Thanks for your answer! Commented Jan 30, 2021 at 17:12
  • Make sure your cursor is inside the containing tags, like right after the closing of the beginning tag...
    – Qasim
    Commented Jul 26, 2021 at 6:14
  • If you don't want braces/parenthesis/quotes/etc to be selected as part of the block, set Text Editor > Auto Surround to never in your settings ("editor.autoSurround": "never")
    – victmo
    Commented Mar 27 at 18:25
13

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

2
  • It would be great being able to do it the Eclipse way - by double clicking inside a section block.
    – godo
    Commented Sep 15, 2022 at 6:21
  • It's shifkey ⇧Shift+⌘Command+P to open the Command Palette now.
    – STO
    Commented Aug 21 at 6:07
9

Try the expand-region extension. It currently works for JavaScript and HTML. To select ever-increasing or decreasing scope.

8

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.)

6

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

1
  • Unfortunately this only works with HTML tags, not for expressions in parentheses or braces.
    – Jo Liss
    Commented Oct 27, 2022 at 16:07
3

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

2
  • 1
    Can you expand on "bind it to whatever keys"? For example, instructions and/or a reference on how to do it. (But without "Edit:", "Update:", or similar - the answer should appear as if it was written today.) Commented Jun 28, 2022 at 12:25
  • @PeterMortensen OK, I posted a new answer, is it the right way to do it? Commented Jul 1, 2022 at 12:04
3

⌃⇧⌘← or ⌃⇧⌘→is also useful for this purpose.

0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.