Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select everything between matching brackets in VS Code

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.

like image 573
Michael Hoeller Avatar asked Jun 15 '16 12:06

Michael Hoeller


People also ask

How do I Select all matching text in VS Code?

Ctrl+D selects the word at the cursor, or the next occurrence of the current selection. Tip: You can also add more cursors with Ctrl+Shift+L, which will add a selection at each occurrence of the current selected text.

How do you Select similar elements in VS Code?

Alt+Enter --> Select all matches. Left arrow --> Ajust cursors.

How do you match braces in Visual Studio?

Put your cursor before or after the brace (your choice) and then press CTRL + ] . It works with parentheses ( ), brackets [ ] and braces { }. From now on you don't need to play Where's Waldo? to find that brace. With the above shortcut, you can also hold SHIFT to select.


2 Answers

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

like image 150
Wosi Avatar answered Sep 25 '22 12:09

Wosi


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.

like image 45
Mark Avatar answered Sep 23 '22 12:09

Mark