How to Replace a Word in VS Code (One File or All Files)

To replace a word in VS Code, open the file and press Ctrl + H (Option + Command + F on a Mac). Type the word you want to find in the first box and the new word in the second box, then press Enter to replace one match at a time or click Replace All (or press Ctrl + Alt + Enter) to replace every match in the file. To replace a word across your whole project, press Ctrl + Shift + H to open Search & Replace in all files.

Below are step-by-step instructions, the options that control what gets matched, how to replace only part of a file, and how to use regular expressions safely.

Replace a word in the current file

  1. Open the file in VS Code.
  2. Press Ctrl + H on Windows and Linux, or Option + Command + F on a Mac. The Find and Replace widget opens at the top right of the editor.
  3. Type the word to find in the Find box. VS Code highlights every match and shows a count, such as “3 of 12.”
  4. Type the replacement in the Replace box.
  5. Press Enter (or click the Replace icon) to replace the current match and jump to the next one.
  6. Click Replace All to change every match at once.

If the Replace box isn’t showing, click the small arrow on the left of the Find widget to expand it.

Match only what you want

Three buttons in the Find box change how matching works:

Button Shortcut What it does
Aa (Match Case) Alt + C Only matches the exact capitalization
ab (Match Whole Word) Alt + W Skips matches inside longer words, so “cat” won’t match “category”
.* (Use Regular Expression) Alt + R Treats the search as a regex pattern

On a Mac, use Option + Command + C, W and R. There’s also a Preserve Case button in the Replace box that keeps the original capitalization, so replacing “user” with “account” turns “User” into “Account.”

Replace only in a selection

  1. Select the lines you want to change.
  2. Press Ctrl + H.
  3. Click the Find in Selection button (the lines icon) or press Alt + L.
  4. Replace as usual. Only matches inside the selection change.

Replace across all files

  1. Press Ctrl + Shift + H (Command + Shift + H on a Mac), or click the Search icon in the Activity Bar.
  2. Enter the search term and the replacement.
  3. Click the three dots to show files to include and files to exclude, such as src/**/*.js or node_modules.
  4. Review the results. Hover over a match to replace or dismiss it individually.
  5. Click Replace All (the icon next to the Replace box) and confirm.

Replacing across files changes them on disk. Commit your work in Git first so you can undo easily.

Rename a variable the smart way

For code symbols like variables, functions and classes, use Rename Symbol instead of text replace. Click the name and press F2, type the new name, and press Enter. VS Code updates real references across files without touching matching text in comments or strings, for languages that support it.

Edit every match with multiple cursors

Select a word and press Ctrl + Shift + L (Command + Shift + L on a Mac) to put a cursor on every occurrence in the file, then type to change them all. Press Ctrl + D repeatedly to add matches one at a time.

Use regular expressions

Turn on regex with .*, then use capture groups. For example, find (\w+)@example\.com and replace with [email protected] to change email domains while keeping usernames. Check the highlighted matches before clicking Replace All.

Related: turn on word wrap in VS Code.

Related: download VS Code in Windows 11.

Related: best code editors for Mac.

Frequently asked questions

How do I undo Replace All?

Press Ctrl + Z in the file. For changes across many files, use Git or undo in each file.

Why doesn’t Replace All change some matches?

Match Case or Whole Word may be on, or the matches are outside the current selection.

Can I replace with a line break?

Yes. Turn on regex and use \n in the Replace box.