ChatGPT解决这个技术问题 Extra ChatGPT

How do I find and replace all occurrences (in all files) in Visual Studio Code?

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.

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

b
banan3'14

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:

https://i.stack.imgur.com/uC2LF.png

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:

https://i.stack.imgur.com/VolXK.png

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:

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

https://i.stack.imgur.com/3mccX.png

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)

https://i.stack.imgur.com/dwnk4.png

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)

https://i.stack.imgur.com/J0t6i.png

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:

https://i.stack.imgur.com/HiyU3.png


Is it possible to save a 'search-replace'? I often search for 'class=' and replace with 'className='
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.
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.
God damn that's dangerous, changing all files as default. Is there a command to just find/replace in a single file?
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.
G
Gangula

Since version 1.3 of vscode this is possible

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

https://i.stack.imgur.com/WAMqQ.gif


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?)
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 See gif for ux search and replace
Oh I see it now its a small button on the right with "ab"
Z
ZF007

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

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


Note: You have to press enter for the search to run when using Ctrl+Shift+H.
j
jhovanec

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:

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.

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


The general issue right now is, that after 10000 results, VSCode stops searching. Any workarounds for that? I.e. iterative search + replace addon?
D
DonDaniel

This is the best way.

First put your cursor on the member and click F2. 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/


what if i want to rename occurrences in only current file?
@temirbek i have been following this request for a while. It's been 2+ years and it hasn't been implemented yet. github.com/Microsoft/vscode/issues/14836
Hi, does it also rename the related folders and import?
M
Magnus Lind Oxlund

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


J
Jesse H.

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

https://i.stack.imgur.com/VBhh8.png

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.


S
Shuaib Abubakker Bapputty Haji

There are 2 methods technically same

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


D
Dharman

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.