To find and replace in all files in VS Code, open the folder, press Ctrl+Shift+H, type the search text and the replacement, optionally limit the scope with files to include and files to exclude, review the results list, and click Replace All. VS Code searches every file in the open folder and its subfolders, except files excluded by your settings and .gitignore.
This guide covers each control in the Search view, regex replacements with capture groups, how to preview and skip individual matches, and the situations where VS Code is not the best fit.
Step by step
- Open the folder. Use File → Open Folder. VS Code only searches the folder (or workspace) that is open, not your whole drive.
- Open search with replace. Press Ctrl+Shift+H. If you opened search with Ctrl+Shift+F, click the small arrow to the left of the search box to reveal the replace field.
- Type the search text. Use the three toggles at the end of the box: Match Case (Alt+C), Match Whole Word (Alt+W) and Use Regular Expression (Alt+R).
- Type the replacement. The results list now shows each match with the old text struck through and the new text beside it.
- Limit the scope. Click the ... (Toggle Search Details) to show files to include and files to exclude.
- Review. Click a result to open a diff view of that file. Hover over a match or file and click the X to dismiss it, so it will not be replaced.
- Replace. Click the Replace All button next to the replace box, or press Ctrl+Alt+Enter, and confirm.
Include and exclude patterns
The two boxes take glob patterns, separated by commas:
| Pattern | Meaning |
|---|---|
*.html | HTML files anywhere in the folder |
*.{html,css,js} | HTML, CSS and JS files |
src/** | Everything under the src folder |
**/vendor/** | Any vendor folder at any depth (useful in exclude) |
*.min.js | Minified files (useful in exclude) |
Next to files to exclude there is a Use Exclude Settings and Ignore Files toggle. When it is on (the default), VS Code also skips anything in your files.exclude and search.exclude settings and anything listed in .gitignore. That is why folders like node_modules do not show up. Turn it off if you are missing matches in files you know exist.
You can also right-click a folder in the Explorer and choose Find in Folder... to fill in files to include for you.
Regex find and replace in VS Code
Turn on regex with Alt+R or the .* button. VS Code uses JavaScript-style regular expressions, and in the replace box you refer to groups with $1, $2 and so on, and to the whole match with $&.
Examples
- Reorder dates: find
(\d{2})/(\d{2})/(\d{4}), replace with$3-$2-$1. - Swap attribute quotes: find
class='([^']*)', replace withclass="$1". - Add a prefix to a function name: find
\bgetUser\(, replace withlegacyGetUser(.
The replace box also supports case-changing modifiers before a group: \u and \l change the next character, and \U and \L change the rest of the group. For example, \U$1 inserts group 1 in capitals.
For a deeper explanation of groups, including named groups, see regex replace with capture groups.
Multi-line search
With regex on, you can match across lines by including \n in the pattern. VS Code handles Windows line endings for you in this case, so \n matches a CRLF line break in search. To type a real line break in the search box itself, press Shift+Enter. Our guide to replacing multiple lines of text in multiple files has patterns for whole blocks.
Preserve case
The Preserve Case toggle (AB) next to the replace box adapts the replacement to the case of each match. With it on, replacing widget with gadget turns Widget into Gadget and WIDGET into GADGET. It is handy for renames in prose and comments.
What to watch out for
- Replace All saves files. VS Code applies the change to files on disk. You can usually undo with Ctrl+Z straight away while focus is on the search results, but once you close the editor or make other changes, recovery means Git or a backup. Commit before a big replace.
- Large result sets are trimmed. VS Code limits how many results it shows (the
search.maxResultssetting, 20,000 by default). If you hit the limit, narrow the scope. - Only the open folder. To change files across several unrelated folders, you need to add each one to a workspace or run the job several times.
- Encoding. VS Code reads files using
files.encoding(UTF-8 by default) unlessfiles.autoGuessEncodingis on. Files in other encodings may not match accented text as expected.
When a dedicated tool fits better
VS Code is excellent for changes inside a project you are already working in. It is less suited to jobs that sit outside a codebase, such as a docs share, a folder of config files on a server drive, or website files from several sources, and to jobs you want to repeat or review afterwards.
Bulk Replace works on any folder without opening it as a project. You set the folder, include sub-folders, and filter by extension, size, modified date and hidden files. In Bulk Replace you can run many rules in a single pass (typed in or imported from CSV), preview a before/after diff of every file, untick matches, and apply. Each run is backed up first, so one click undoes it, and a timestamped CSV report records which files changed and how many matches each had. You can also save the folder, filters and rules as a named project to re-run later.
Tip: whichever tool you use, run the search once without a replacement and check the total match count before replacing. If the number surprises you, your pattern needs tightening.
Search Editor for big result sets
For large searches, the command Search Editor: Open New Search Editor shows results in a full editor tab instead of the side bar, with a few lines of context around each match. It is easier to read through hundreds of results there before deciding what to replace.
Quick reference
- Search across files: Ctrl+Shift+F
- Replace across files: Ctrl+Shift+H
- Toggle match case / whole word / regex: Alt+C / Alt+W / Alt+R
- Replace All in search: Ctrl+Alt+Enter
- Groups in the replacement:
$1, whole match:$&
If you are comparing approaches for a big job, our overview of how to find and replace text in multiple files sets VS Code alongside PowerShell and batch tools.
Frequently asked questions
What is the shortcut for replace in files in VS Code?
Ctrl+Shift+H opens the Search view with the replace box showing. Ctrl+Shift+F opens search, and the arrow on the left expands the replace box.
How do I use capture groups in VS Code replace?
Turn on regex with the .* button or Alt+R, wrap part of the pattern in parentheses and use $1, $2 in the replace box.
Why is VS Code not finding matches in some files?
Files excluded by files.exclude, search.exclude or .gitignore are skipped by default. Check the exclude settings, or turn off Use Exclude Settings and Ignore Files in the files to exclude box.
