How to change or replace all occurrences of a word in VSCode
November 13, 2022
VSCode Tip: How to change or replace all occurrences of a word in VSCode:
Suppose you want to change all the occurrences of a word in VSCode, you can do it in many ways. In this post, I will show you 3 ways to do that.
Method 1: Search and replace:
You can search for all the words in a file and replace these. Open the file in VSCode and click on Ctrl + F for windows or Cmd + F for Mac. It will open the search box in the file.
If you click on the right arrow >, it will show you one more box.
You can enter the text to search in the Find box and text to replace in the Replace box.
It will highlight all matches. If you press enter, it will replace the current highlighted text with the replacement text. You can also use the top-bottom arrows to move between the matches.
If you want to replace all matches in the current file, you can click on the bottom-right icon or you can press Ctrl + Alt + Enter on Windows or Cmd + Alt + Enter on Mac.
If you select a text and open the search bar, it will add that text in the Find box.
Method 2: Open the search and replace box directly:
Instead of opening the search box and toggling between the replace box, you can also press Ctrl + H
on Windows or Cmd + H
on Mac to open the box directly. You can also select a text and press the keys to open the search and replace box.
It is similar to the first method I explained. You can enter the text you want to replace and either replace each word one by one or replace all words in that file.
Method 3: Select multiple words in the editor and replace:
This is another approach to select and replace multiple words in a file. Select one word or words and press Ctrl + D
on Windows or Cmd + D
on Mac. It will find and select the next matching word.
You can keep pressing Ctrl + D
or Cmd + D
to move the selection to the
next matching.
In the above example gif, I have selected the word search
and using Ctrl + D
to select the next word.
Once your selection is done, you can enter anything on your Keyboard to replace these words.
Method 4: Replace words in all files in a workspace repository:
You have to add Shift to the above key combinations to search and replace in all the files in the current repository. You can use Ctrl + Shift + F
on Windows or Cmd + Shift + F
on Mac to search in all the files in the current repository. Similarly, you can use Ctrl + Shift + H
on Windows or Cmd + Shift + H
on Mac for search and replace.
It will open the search box in a new window to the left and you can use it to search and replace all matching words.