589

I can't figure out how to find and replace all occurrences of a word in different files using Visual Studio Code version 1.0.

I get the impression this should be possible since doing Ctrl + Shift + F allows me to simply search a folder, but i am clueless to how to proceed from here.

I looked in various key combination https://code.visualstudio.com/Docs/customization/keybindings

But I only found how to replace all occurrences in the open file (Ctrl + h)

All help gratefully appreciated, this is driving me crazy.

ZF007
  • 3,708
  • 8
  • 29
  • 48
Cisum Inas
  • 11,552
  • 11
  • 40
  • 55
  • In all notes here the simple text "replace in files" doesn't disambiguate between open and closed files. In the current VSCode when you open the Find-All/Replace-All view there is a book icon for "Search only in open editors". Note also that your "files to include" and "files to exclude" _must_ be crafted carefully. Another icon there is "Use exclude settings and ignore files". When active you may not see everything you want - or you may see files you don't want. Finally, note the Refresh icon at the top, which may be required after changing other settings here. HTH – TonyG Jul 07 '21 at 17:56

12 Answers12

743

I'm using Visual Studio Code 1.8, and this feature is available. But it's a little tricky to understand at first, and (at the time of writing) the docs don't explain clearly how to use it, so here's how it works, step by step:

Invoke Replace in Files (under the Edit menu, or with shortcut Ctrl+Shift+H)

You'll see a standard Find/Replace input replacing the files pane on the left:

enter image description here

Enter your search string and the replace string, then press enter. It may churn for a second searching all files, then it'll show the proposed changes in all your project files -- but note, these changes haven't been made yet! Here's what it looks like:

enter image description here

Now you need to make the changes (and even after that, you have to save the modified files.)

You can make those changes in various ways:

  1. Make all changes to all files at once.

Click the replace icon next to your replace string (note: you'll get a dialog to confirm this bulk action.)

enter image description here

  1. Make all changes in a single file at once.

Click the replace icon next to the filename (note: the icon only shows up when you hover over the filename row)

enter image description here

  1. Make a single change in a single file.

Click the replace icon next to the individual change: (note: the icon only shows up when you hover over the change row)

enter image description here

Finally, don't forget to save!

All those files are now modified in the editor and not yet saved to disk (unless Auto Save is on).

Use File -> Save All (or Ctrl+Alt+S)

Update: I'm not sure when this was added, but if you click the "Replace all" button and see this dialog, clicking "Replace" will change and save all files in one click:

enter image description here

banan3'14
  • 3,810
  • 3
  • 24
  • 47
Jeff Ward
  • 16,563
  • 6
  • 48
  • 57
  • 1
    Is it possible to save a 'search-replace'? I often search for 'class=' and replace with 'className=' – Rune Jeppesen Aug 28 '17 at 07:49
  • 1
    Is it possible to skip the last step and do a silent search and replace? If I'm doing a trivial search and replace, I don't want to save each individual file. – kayasky Sep 07 '17 at 14:20
  • 1
    I'm not sure the answer to either of these questions. But @satJ, note that `Ctrl+Alt+S` saves all modified files at once, so it's pretty quick and easy. – Jeff Ward Sep 08 '17 at 13:27
  • God damn that's dangerous, changing all files as default. Is there a command to just find/replace in a single file? – Alexander Mills Nov 20 '17 at 23:56
  • The current save all default is Ctrl-k, followed by the letter s. Ctrl-k, followed by Ctrl+w will close all your windows after the programs has opened a bunch of them. – Rebecca Feb 09 '18 at 16:47
  • In VS Code 1.31.1 (and possibly earlier) you can exclude files or lines of code from your find & replace operation by clicking the "close" icons in the file search results before you click replace. – Wireblue Feb 27 '19 at 01:02
  • I would prefer not to click the actual change, but while standing on a replace result line (a candidate for replacing) to have a shortcut to accept it and a shortcut for declining it. Do these exist? I looked for it but can not find any... – musicformellons May 25 '19 at 14:26
  • @musicformellons - maybe not exactly what you're looking for, but it would appear that you can navigate the "proposed changes" pane with the arrow keys, and use `del` to reject a change, or `ctrl-shift-1` to accept a change. A little cumbersome, but perhaps usable. – Jeff Ward May 28 '19 at 16:56
  • @JeffWard That's nice, thank you! Maybe you also know the shortcut to get from the search/ replace box to the "proposed changes"? – musicformellons May 28 '19 at 20:24
  • @musicformellons you can do it all via the keyboard: `ctrl-shift-h`, type search string, `tab`, type replace string, `enter` performs the search,`tab` about seven times to highlight the proposed changes pane, `down arrow` starts moving through proposed items. – Jeff Ward May 29 '19 at 14:40
268

Since version 1.3 of vscode this is possible

  1. Navigate to the search, click icon to the left or:
    • (mac) cmd + shift + h
    • (PC) ctrl + shift + h
  2. expand replace
  3. enter search term and replace term
  4. confirm!

Search and replace with vscode

Gangula
  • 5,193
  • 4
  • 30
  • 59
Cisum Inas
  • 11,552
  • 11
  • 40
  • 55
  • 1
    To search stuff in a specific folder just add the folder path in the field `files to include`. You can find/replace stuff even outside the workspace. ⚠ it doesn't search within file names, but within the files. (Anyone knows how to search file name outside the workspace?) – JinSnow Sep 06 '19 at 14:04
  • 1
    2021 its still not implemented, there is no button to click "replace-all" you need to use a keyboard shortcut... I guess I shouldn't completely replace sublime-text – ta32 Jun 29 '21 at 07:40
  • 1
    @ta32 See gif for ux search and replace – Cisum Inas Jul 14 '21 at 21:21
  • 2
    Oh I see it now its a small button on the right with "ab" – ta32 Jul 15 '21 at 01:49
57

To replace a string in a single file (currently opened): CTRL + H

For replacing at workspace level use: CTRL + SHIFT + H

ZF007
  • 3,708
  • 8
  • 29
  • 48
Sidd501
  • 585
  • 4
  • 7
39

Update for 2020

If you are using the search feature to search across files (Ctrl + Shift + F) it can be easy to miss how to convert your search to a search and replace within the UI.

Here's a typical search result:

search result image

To convert this to a search and replace you need to click the arrow icon to the left of the search input field. This will open the replace options as seen below. Note the arrow icon is now pointed down.

search and replace image

The keyboard shortcut Ctrl + Shift + H will also work as well to access the search and replace.

Link to VSCode docs on search and replace: https://code.visualstudio.com/docs/editor/codebasics#_search-and-replace

jhovanec
  • 796
  • 8
  • 8
  • 1
    The general issue right now is, that after 10000 results, VSCode stops searching. Any workarounds for that? I.e. iterative search + replace addon? – Jan Dec 09 '20 at 13:28
31

This is the best way.

  1. First put your cursor on the member and click F2.

  2. Then type the new name and hit the Enter key. This will rename all of the occurrences in every file in your project.

This is ideal for when you want to rename across multiple files. For example, you may want to rename a publicly accessible function on an Angular service and have everywhere that uses it get updated.

For more great tools I highly recommend: https://johnpapa.net/refactoring-with-visual-studio-code/

Snackdex
  • 674
  • 8
  • 18
9

On the Visual Studio Code Key Bindings page, the section Keyboard Shortcuts Reference has links to a PDF for each major OS. Once open, search for "replace in files" or any other shortcut you might need.

Another way is to use the Command Palette (ctrl/cmd+shift+P) where you can type "replace" to list all related commands, including the one you want:

Replace in Files ctrl/cmd+shift+H

Magnus Lind Oxlund
  • 304
  • 1
  • 6
  • 19
T04435
  • 12,507
  • 5
  • 54
  • 54
8

Visual Studio Code: Version: 1.53.2


If you are looking for the answer in 2021 (like I was), the answer is here on the Microsoft website but honestly hard to follow.

Go to Edit > Replace in Files

enter image description here

From there it is similar to the search funtionality for a single file.

I changed the name of a class I was using across files and this worked perfectly.

Note: If you cannot find the Replace in Files option, first click on the Search icon (magnifying glass) and then it will appear.

Jesse H.
  • 465
  • 4
  • 12
6

Step by step with images

Nowadays in VS Code this can be done quickly.

1) Click on the Search Button (in the side bar):

enter image description here


2) Enter in the inputs what do you want to find, and with what do you want to replace it:

enter image description here

(Here I am replacing in my project all the occurrences of table-cell with t-cell)


3) Finally, click on this small button (Replace All):

enter image description here

Then a pop-up will appear to confirm if you want to replace all occurrences. Click on Replace.

And you're done!

enter image description here

underflow
  • 1,545
  • 1
  • 5
  • 19
1

There are 2 methods technically same

  1. First put your cursor on the word and press F2. Replace your word and press Enter.

  2. First put your cursor on the word and left click it. Click "Rename Symbol" option. Replace your word and press Enter.

1

In the VSC version 1.57 you can use the F2 key to replace on the folder(Rename Symbol)

Only thing is that even though all the places inside folder are replaced the file is not saved and the save has to be done afterwards

Also, instead of presing enter and replace all directly, you can press Shift + Enter to preview the places that are going to be renamed.

Dharman
  • 30,962
  • 25
  • 85
  • 135
e_l_m_t_7
  • 61
  • 1
  • 4
0

For search and replace all use this command: command + option + F

enter image description here

Maninder
  • 1,539
  • 1
  • 10
  • 12
0

ctrl+shift+f (search box appear in left side type your word in the search box to replace) ctrl+shift+h (type the word to replace with) select replaceAll /replace accordingly TO REPLACE.