589

Are there any tricks/extensions to select all instances of selected word(s) in Visual Studio Code to facilitate editing or deleting those instances without search and replace?

Perhaps something similar to Alt+F3 in Sublime Text?

Penny Liu
  • 15,447
  • 5
  • 79
  • 98
Alaa M. Tekleh
  • 6,938
  • 4
  • 16
  • 29

14 Answers14

1061

Select All Occurrences of Find Match editor.action.selectHighlights.

Ctrl+Shift+L

Cmd+Shift+L or Cmd+Ctrl+G on Mac

Anand Singh
  • 1,091
  • 13
  • 21
Alex
  • 59,571
  • 22
  • 137
  • 126
  • My vs code didn't have this command setted, so I had to set it myself and then it works. – Tito Leiva Apr 15 '19 at 10:55
  • thanks. I wish I could get VS Code on Mac to report on keybindings using these keys instead of "CMD F2" which I'm not even clear on how to use – Dan Rosenstark Jun 08 '19 at 16:19
  • what about select a word and then update all those words at a time as in sublime? I get that vscode selects way to much with that command and also it would be nice to be able to have a keystroke to tell vscode which ones we DO want to select. – Charlie Parker Apr 15 '20 at 21:52
  • cool! Any way to select just the contents of a regex capture group instead of the whole match? eg: `href="(.*)"` – solidau Apr 17 '20 at 22:34
  • 3
    @solidau very late to the party, but you could change your regex to use positive look behind and positive look ahead `(?<=href=")(.*)(?=")` so then the whole match is actually just the centre bit. technically you don't even need `(.*)` at this point but just `.*` but just be clear if you want to be greedy or not with that regex – WORMSS Nov 09 '20 at 15:01
  • If you don't have that shortcut, you can press Ctrl + Shift + P and type "Select All Occurrences of Find Match" then run it by pressing Enter. – totymedli Nov 27 '20 at 10:35
  • Also, if you want to add next occurance to the selection one by one, then you should use `Ctrl + D` successively. – Noopur Phalak Jan 21 '21 at 14:25
  • This is not quite correct. You need to highlight the phrase you are searching for first. – alex Oct 19 '21 at 18:08
  • Okay, for me it only works when find window is open. So the process would be Cmd + f -> Put in text you want to find -> Cmd + Shift + L – Mayur Chauhan Feb 01 '22 at 00:04
  • 2
    The find window CANNOT be open for this to work. You need to go into the text editor, highlight some phrase, and *then* press `Cmd+Shift+L`. – alex Feb 16 '22 at 17:35
  • Thanks, Alex. Yes, it worked after selecting the word and Cmd + Ctrl + G. Sorry for creating the confusion, IDK why it didn't work last time or maybe I wasn't trying it in the right way. – Mayur Chauhan Mar 30 '22 at 01:18
122

According to Key Bindings for Visual Studio Code there's:

Ctrl+Shift+L to select all occurrences of current selection

and

Ctrl+F2 to select all occurrences of current word

You can view the currently active keyboard shortcuts in VS Code in the Command Palette (View -> Command Palette) or in the Keyboard Shortcuts editor (File > Preferences > Keyboard Shortcuts).

j08691
  • 204,283
  • 31
  • 260
  • 272
  • 1
    It does select different words too. For example: when I select `name`, then it will also select the same strings in the `named`. – Ari Mar 13 '19 at 04:31
  • if there is a space after your word, you can select the space after your word as well to just get that word (e.g. `name `) – Hansang Jun 16 '19 at 05:44
  • Just a reminder that it's limited to 10000 cursors, if you're planning on messing with big files. – Digger Jan 19 '23 at 20:00
114

What if you want to select just a few?

No problem, first:

  1. Ctrl+F find the letters by typing them
  2. ESC to quit searching
    (you need to do this even when using Ctrl+Shift+L to select all occurences)

OR

  1. just select those letters with your mouse or keyboard (Shift+arrows)

Now that the mouse cursor is blinking on your first selection, using a few more Key Bindings (thanks for the ref j08691) you may:

  • Ctrl+D select (add) the next occurrence
  • Ctrl+K+Ctrl+D skip the current/latest occurrence and add the next occurrence
  • Ctrl+U undo the latest cursor operation
Henke
  • 4,445
  • 3
  • 31
  • 44
CPHPython
  • 12,379
  • 5
  • 59
  • 71
  • 1
    This doesn't answer the original question, and is misleading. The find window CANNOT be open on MacOS (at least) to select all occurrences of a phrase. It just won't work. You need to go into the text editor, highlight some phrase, and *only then* press `Cmd+Shift+L`. – alex Feb 16 '22 at 17:36
  • 1
    @alex perhaps you misread what I wrote: I'm using a Mac and to open the "find window/popup" you do `Cmd`+`F`, type the phrase, then press `ESC` (to close the popup) and only then `Cmd`+`shift`+`L` to select all occurrences. – CPHPython Feb 16 '22 at 18:21
  • 1
    Yeah, I definitely misread this - it still answers a kind of different question, but I'll undo the downvote. Thanks! – alex Feb 16 '22 at 20:17
47

on Mac:

select all matches: Command + Shift + L

but if you just want to select another match up coming next: Command + D

Anthonyeef
  • 2,595
  • 1
  • 27
  • 25
18

If you want to do one by one then this is what you can do:

  1. Select a word
  2. Press ctrl + d (in windows).

This will help to select words one by one.

thatguy
  • 21,059
  • 6
  • 30
  • 40
Arjjun
  • 1,203
  • 16
  • 15
18

On Ubuntu:

Ctrl + F2 will select all occurrences immediately.

Ctrl + D will select occurrences one by one.

Rubel
  • 1,255
  • 14
  • 18
11

In my MacOS case for some reason Cmd+Shift+L is not working while pressing the short cut on the keyboard (although it work just fine while clicking on this option in menu: Selection -> Select All Occurences). So for me pressing Cmd+FN+F2 did the trick (FN is for enabling "F2" obviously).

Btw, if you forget this shortcut just do right-click on the selection and see "Change All Occurrences" option

Alexey
  • 1,914
  • 16
  • 13
11

Several options have been listed, but a couple are missing. Its possible to use the rename and refactoring tools, for not just selecting all, but making specific changes, after everything has been selected. I am going to try and bundle all answers I feel are relevant together, and add two more that, not only get the job done, but are really great tools for making single changes over multiple occurrences of the same code.



1. Select All Matches


To use multiple cursors to select all matches, you can use 1 of 2 keybindings. Both keybindings preform the same functionality, so in other words, 2 keybindings, 1 vscode command. The keybindings are as follows.

1. CTRL + F2
2. CTRL + SHIFT + L
Default Keybinding
{
  "key": "ctrl+f2",
  "command": "editor.action.changeAll",
  "when": "editorTextFocus && !editorReadonly"
}

// You can view/customize VSCode keybindings by pressing F1 and typing Keybindings



2. Select Next Match


You can Select Next Match by using the keybinding below. This is good for selecting all occurrences within a specific vicinity.

CTRL + D

Default Keybinding
{
  "key": "ctrl+d",
  "command": "editor.action.addSelectionToNextFindMatch",
  "when": "editorFocus"
}
 
SIDE NOTE
If you press the keybinding CTRL + K before pressing CTRL + D, instead of selecting the next instance, it will move you to the next instance, and target it with your cursor.


Default Keybinding

3. Rename


This one hasn't been mentioned yet, but this one is what you would except when doing a refactor in a traditional IDE, like Visual Studio 2022, or JetBrains: IntelliJ.

When you use this keybinding, it attempts to solve some of the problems that are incurred when using the other options listed above. The other options are overly greedy sometimes, and select parts of words that you didn't want to select, and if your not careful, you can delete quite a bit of code, resulting in messy situation. If you didn't notice that you screwed up right away, you end up saving, or working for a long while before having to reset everything, and you end up loosing a lot of work & time.

F2 attempts to solve this problem by implementing logic under the hood (IDK if its an algorithm or what it is) but it feels very much like refactor in Visual Studio. It only selects specific cases that you would want to target. So if a varable is named foo, is won't select foo from a var named fooFoo. It also won't select foo from a comment.

F2

{
  "key": "f2",
  "command": "editor.action.rename",
  "when": "editorHasRenameProvider && editorTextFocus && !editorReadonly"
}



4. Find


This one I will keep short and sweet, but find will iterate through ever occurrence of a pattern that you type into the editor-widget.

Press CTRL + F then type what you want to select. Keep pressing ENTER until you find it.



5. The Best For Last


There is not one method for selecting all occurrences that is the best every time, that's why there are several different methods for doing it, however, there are some methods that are generally more useful than others. This feature, as far as I know, is unique to VS Code, and I use it all the time when working with large JSON files, and Large Code Bases (like an Open Source PR).

VS Code has its own search editor, its a special editor. You can open the search editor by pressing on the Magnifying Glass Icon on the Activity-bar. Type into the search editors side bar text-input what it is you want to select, then press ENTER. It will return all the results in the sidebar. You can use the lower text input, to replace all of the results with what ever you like. You can also click OPEN IN THE EDITOR (it looks like a link) and it will reproduce everything you searched for in a new document, that is opened to the side. From there you can manipulate it, and add it back to the document. I've already written enough for one answer, so I am not going to go to deep into every thing it can do, but this not only selects everything, it also extracts it, replaces it, lets you nit-pick exactly what it means to select "all" of a specific occurrence. It's a great tool for making a single change in a recursive fashion.

JΛYDΞV
  • 8,532
  • 3
  • 51
  • 77
10

Ctrl+Shift+L to select all occurrences of current selection.

enter image description here

8

This seems an old question, but it worth an answer.

There is - besides the accepted answer - a fancy shortcut to do this, just select the desired word and press Ctrl + D as many times as desired, each press will select an exact occurrence in the editor, after all occurrences are selected, just type the replacement and all the occurrences will be replaced as you type.

AEM
  • 1,354
  • 8
  • 20
  • 30
Ahmed Suror
  • 439
  • 6
  • 17
6

Ctrl + F2 works for me in Windows 10.

Ctrl + Shift + L starts performance logging

Ricky Poon
  • 87
  • 1
  • 3
6

I had another application, called Loom, running in the background on my Mac which was taking over my keyboard bindings.

Cmd+Shift+L

It was just random that I figured out it was preventing the keyboard bindings from reaching VS Code. I tried to turn off applications that I had open one by one. Not the best approach, but it worked.

John
  • 10,165
  • 5
  • 55
  • 71
4

I needed to extract all the matched search lines (using regex) in a file

  1. Ctrl+F Open find. Select regex icon and enter search pattern
  2. (optional) Enable select highlights by opening settings and search for selectHighlights (Ctrl+,, selectHighlights)
  3. Ctrl+L Select all search items
  4. Ctrl+C Copy all selected lines
  5. Ctrl+N Open new document
  6. Ctrl+V Paste all searched lines.
Rohit
  • 6,365
  • 14
  • 59
  • 90
-1

Additionally to unselect use Cntrl + U