Select everything between matching brackets in VS Code

Visual Studio-CodeKeyboard Shortcuts

Visual Studio-Code Problem Overview


Is there in VS Code a shortcut available which selects all code between matching brackets? E.g. in Atom, this shortcut is ctrl+alt+m.

Visual Studio-Code Solutions


Solution 1 - Visual Studio-Code

Place the cursor inside the bracket (not directly next to a bracket) and execute editor.action.smartSelect.grow until everything inside the bracket is selected. The default shortcut is Shift+Alt+Right (or Ctrl+Shift+Right on Mac).

You can shrink the selection with editor.action.smartSelect.shrink which has the default shortcut Shift+Alt+Left (or Ctrl+Shift+Left on Mac).

Solution 2 - Visual Studio-Code

I still think the expand region extension is the way to go on this. But vscode 1.20 did add the "Select to Bracket" command (see command palette). It has no default keybinding but the command is

editor.action.selectToBracket

so you can assign your own binding. But it is not as handy as the extension. The smartSelect options mentioned above give me unexpected/unwanted results.

---------- Update ---------------

editor.action.smartSelect.expand command was added at some point after the answers here.

Bound to Shift+Alt+RightArrow. Will progressively expand the selection including to within and not including the brackets, parentheses if you wish. You will need to keep triggering the RightArrow to get to where you want. Will not select only within the brackets if you start right next to a bracket - start anywhere else and it will.

Solution 3 - Visual Studio-Code

I suggest to remap those keys to another key, this key combination is popular, sometimes it doesn't work because of the conflict with other apps.

Open the key binding setting, type the smartselect to find this setting, grow is expand the selection from the cursor current position to the nearest bracket, press multiple times if you want to expand or shrink to the next bracket.

enter image description here

enter image description here

Solution 4 - Visual Studio-Code

As answered by Mark, editor.action.selectToBracket works. I assigned keybindings to

ctrl+shift+m

as keyboard shortcut. This used to be default in Older version of VS.

Note: Ctrl+m is shortcut for "Go to bracket"

Solution 5 - Visual Studio-Code

I am using this extension, called Quick and Simple Text Selection - yes, that's the name :)

If you don't want to use an extension, this is the best I come up with so far: go to edit keyboard shortcuts and from there open your keybindings.json file and add the following lines

{
    "key": "ctrl+'",
    "command": "editor.action.smartSelect.grow",
    "when": "editorTextFocus"
}

Solution 6 - Visual Studio-Code

In MacOS: it's shift + control + right, you hit right a few time to expand the smartSelect.

  • To find your own settings related to smartSelect, go to Keyboard Shortcuts or use command + k command + s.

  • You can check VSCode Keyboard Shortcuts Reference by typing command + k command + r. It will lead you to this page.

Solution 7 - Visual Studio-Code

editor.action.smartSelect.expand, assigned to ctrl+shift+right will expand your current selection, so inside a string, will expand to the current word, and to the whole string when pressed again. I think this is the proper shortcut to learn because it can also expand beyond the string to select ever more wrappers, which is a pretty neat thing to be able to do.

As of v1.44, there is no keyboard shortcut assigned to editor.action.smartSelect.grow anymore, as mentioned in the current accepted answer by Wosi.

Solution 8 - Visual Studio-Code

Short answer vscode 2021 in Win10 : ctrl + shift + space

-- enter image description here

  "key": "ctrl+shift+space",
  "command": "editor.action.smartSelect.grow",
  "when": "editorTextFocus"

enter image description here

Solution 9 - Visual Studio-Code

The extension BracketSelect does the trick perfectly! It selects all text between the nearest matching quotes or brackets at once. Press it twice, and it adds the delimiting elements (the quotes or brackets) to the selection. Press it once more and the selection grows it's way up.

Basically, it works like the smartSelect command, but with the neat difference that it selects all text inside the brackets or quotes at once, which to me is a way more common use case than the way smartSelect works.

Solution 10 - Visual Studio-Code

I have found that all of the above answers do work, but they also select a space in front of the opening bracket, and selects the closing bracket. Rather than actually selecting only the text between brackets. You can test this by pressing command+[ and it will move an entire conditional, for example, rather than just the code between the brackets. Something I commonly used in Coda when formatting code.

One solution to this I found, is a plugin called Scoper.

This highlights everything between brackets without actually highlighting it. You can set the highlight color. Then I can click inside the first bracket, hold down shift, and click before the closing bracket to actually highlight everything between the brackets, and exclude the brackets themselves.

Many people may not see a use case for this, but thought I'd post just in case it helps anyone as none of the current solutions worked for me.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMichael HoellerView Question on Stackoverflow
Solution 1 - Visual Studio-CodeWosiView Answer on Stackoverflow
Solution 2 - Visual Studio-CodeMarkView Answer on Stackoverflow
Solution 3 - Visual Studio-CodeFinnView Answer on Stackoverflow
Solution 4 - Visual Studio-CodeAbhayView Answer on Stackoverflow
Solution 5 - Visual Studio-CodeStefan GabosView Answer on Stackoverflow
Solution 6 - Visual Studio-CodeVietView Answer on Stackoverflow
Solution 7 - Visual Studio-CodefrandroidView Answer on Stackoverflow
Solution 8 - Visual Studio-CodeNassimView Answer on Stackoverflow
Solution 9 - Visual Studio-CodeRSeidelsohnView Answer on Stackoverflow
Solution 10 - Visual Studio-CodeGlenn PowellView Answer on Stackoverflow