VS Code

How to Find and Replace in VSCode Easily

How to Find and Replace in VSCode Easily

Ctrl+H opens the Find widget with the replace field showing, and it only touches the file you’re in. The whole workspace is Ctrl+Shift+H instead, which opens the Search view. On macOS the shortcuts are Option+Command+F and Shift+Command+H.

There’s a default that surprises people the first time. Microsoft’s VS Code documentation states that search excludes some folders by default, node_modules among them. A project-wide replace leaves those folders untouched unless the exclusion settings change.

How to Find and Replace in the Current File

YouTube player

Ctrl+F (Command+F on macOS) opens the Find widget. Ctrl+H (Option+Command+F) adds the replace field underneath, and from there you click Replace or Replace All.

The table follows the default shortcuts in the VS Code keyboard shortcuts reference, as checked in November 2026.

ActionWindows and LinuxmacOS
FindCtrl+FCommand+F
ReplaceCtrl+HOption+Command+F
Next matchEnterEnter
Previous matchShift+EnterShift+Enter
Select all occurrences of the matchAlt+EnterOption+Enter

Replace one match or every match

Replace swaps the highlighted match and moves on to the next one. Pressing Enter inside the replace field does the same thing, which beats reaching for the mouse each time.

Replace All rewrites every match in the file at once. Ctrl+Alt+Enter (Option+Command+Enter on macOS) runs it from the keyboard.

Matches are highlighted in the editor, and they also show up in the overview ruler and the minimap. Glance at the ruler before you commit to Replace All.

VS Code searches as you type. If you’d rather it waited for Enter, clear the editor.find.findOnType setting.

Limit the replace to selected text

A selection does not limit the replace on its own.

Press Alt+L (Option+Command+L on macOS) or click the Find in Selection icon in the widget, and both find and replace stay inside the highlighted lines.

To make that automatic, set editor.find.autoFindInSelection. A value of always keeps every search inside the selection, while multiline applies it only when several lines are selected. If you would rather type the value than click through the Settings UI, open settings.json and add it there.

What the Find box fills in on its own

Opening the widget fills the input with the selected text, or the word under the cursor if nothing is selected. Check what the box holds before you start typing a replacement.

Setting editor.find.seedSearchStringFromSelection to “never” turns the pre-fill off. Past queries also survive a restart, because history is saved per workspace.

What the Match Options Change

Match Case, Match Whole Word and Regular Expression sit in the find control. Preserve Case sits in the replace control.

OptionEffectWindows and LinuxmacOS
Match CaseTreats Color and color as differentAlt+COption+Command+C
Match Whole WordSkips hits inside longer wordsAlt+WOption+Command+W
Regular ExpressionReads the search as a patternAlt+ROption+Command+R

The Search view has its own commands for those toggles, bound to the same keys.

Why whole word matters

Leave it off and replacing id with userId also catches width, which comes out as wuserIdth. With it on, only the standalone word id changes.

Short search terms are where the damage happens, so I turn Match Whole Word on before anything else.

Preserve Case

Preserve Case carries the capitalization of each match over to the replacement. Replace color with colour and Color becomes Colour while COLOR becomes COLOUR.

It lives in the replace control rather than next to the find toggles, so plenty of people never spot it.

How to Find and Replace Across All Files

YouTube player

Ctrl+Shift+H (Shift+Command+H on macOS) opens Replace in Files. You enter the search term and the replacement, then apply the changes file by file or all at once.

  1. Open the Search view with Ctrl+Shift+F, or jump straight to replace with Ctrl+Shift+H.
  2. Type the search term and set the toggles (Alt+C, Alt+W, Alt+R) if the term needs them.
  3. Type the replacement in the Replace box. A diff preview of the pending changes appears as you type.
  4. Expand a file to preview its hits, and single-click a hit to open it.
  5. Apply the replacement to every file at once, or limit it to all matches in a single file, or to one change.

With a folder open, the search covers the whole project by default. To aim at one folder instead, right-click it in the Explorer and choose Find in Folder. If no folder is open at all, only the open files get searched.

Before a large replace, commit or stash your work. On a clean tree, source control then shows exactly which files the replace touched.

Preview changes before you apply them

The diff preview is there to catch mistakes, so it’s worth using.

Typing in the Replace box brings up a diff preview of the pending changes. Running Replace All on a three-letter word across 40 files is a bad time to find out it also matched a variable name.

Start by skimming the file list for paths you did not expect. Then expand two or three files and read the hits, keeping an eye out for partial-word matches.

Review results in a Search Editor

Results can also open in a full-size editor tab, with syntax highlighting and context lines. The Open New Search Editor button in the Search view does it, and so does the Open in editor link or the Search Editor: Open Results in Editor command.

The number of context lines comes from search.searchEditor.defaultNumberOfContextLines, which defaults to 1. To jump to a hit, F12 opens it in the current group, and Ctrl+K F12 (Command+K F12 on macOS) opens it to the side.

Reach for it when the narrow Search panel hides the context you need to judge a match.

How to Include or Exclude Files and Folders

Ctrl+Shift+J (Shift+Command+J on macOS) toggles the search details, which reveals the files to include and files to exclude boxes. Patterns go in comma-separated, with forward slashes.

A plain name like example matches any folder or file with that name, while ./example matches just the top-level folder. The glob symbols *, ?, **, {}, [] and [!…] all work.

A pairing I’d reach for is *.ts, *.tsx in files to include and **/dist/** in files to exclude. A replace then only reaches source TypeScript.

Where the pattern livesPattern baseApplies to
Files to exclude boxLeading ** assumedThat search only
search.exclude settingWorkspace folder, needs **/ for any depthEvery search in the workspace
files.exclude settingWorkspace folder, needs **/ for any depthExplorer and search

Why node_modules never shows up

VS Code skips some folders by default, node_modules among them, so the results aren’t full of things you don’t care about.

Whether .gitignore, files.exclude and search.exclude apply is decided by the Use Exclude Settings and Ignore Files toggle. Turn it off and none of those rules filter the search anymore.

Because search.useIgnoreFiles defaults to true, .gitignore and .ignore files are respected. Anything a .gitignore file hides stays out of your replace too.

To search inside an excluded path, add a ! entry to search.exclude, for example “!node_modules/@types”: true.

Glob case sensitivity follows the OS

On Windows and macOS, include and exclude patterns are case-insensitive. Linux treats them as case-sensitive.

This is separate from the Match Case toggle, so a pattern can behave differently on a teammate’s machine.

Limit a search to changed files

The Search only in changed files toggle limits results to files with uncommitted changes. It stays disabled when there is no repository or nothing has changed.

After a replace it works as a quick second pass. Search for the old term again and see whether any edited file still holds it.

How to Use Regex in Find and Replace

With Regular Expression switched on (Alt+R, or Option+Command+R on macOS), the find box reads your input as a pattern. Captured groups come back in the replace box as $1, $2 and so on.

The pattern side is standard regex, so a regex cheat sheet covers most of what you need to write it.

Regex searching also works in the search box when you search across files.

Reuse matched text with capture groups

GoalFindReplace
Reverse “Last, First” names([^,]+),\s*(.+)$2 $1
Date to year-first order(\d{1,2})/(\d{1,2})/(\d{4})$3-$1-$2
Rename only let declarations(let\s+)myVariable\b$1myVar

The number after the dollar sign is the group’s order. Count opening parentheses from left to right.

Group only what you want to keep. Anything outside a group gets replaced and drops out of the result.

Change letter case in the replacement

\u uppercases one character and \l lowercases one. For longer stretches, \U uppercases the rest of the group and \L lowercases it.

The modifiers stack, so \u\u\u$1 uppercases the first three characters of group 1.

Snake case to camel case is the classic use. Find ([a-z])_([a-z]), replace with $1\u$2, and user_profile_id becomes userProfileId.

One ranking tutorial, Steve Kinney’s VS Code course, writes the same conversion as $1${2.toUpperCase()}, a JavaScript expression inside the replacement. That page never explains where the expression syntax is supported.

The VS Code docs, checked in November 2026, list $n references and the four case modifiers for replacements. Stick to those, and test anything else on a throwaway file first.

Match across multiple lines

Multiline find and replace uses the same two boxes.

Paste multiline text into the Find or Replace box, or press Ctrl+Enter to insert a new line in the input. With Regular Expression on, \n in the pattern matches a line break. If the boxes feel cramped, drag the widget’s left edge to resize it, or double-click the edge to maximize it.

Which Method Fits the Job?

The Find widget handles one file and the Search view handles many. Multi-cursor is for when you want to watch every edit land, and Rename Symbol (F2) is the one for variables and functions.

MethodScopeBest forMain risk
Find widgetOne fileQuick text swapsReplace All on a common word
Search viewWhole workspaceBulk string or config changesReaching files you never reviewed
Multi-cursorOne fileA handful of edits you want to seeSlow past a dozen matches
Rename Symbol (F2)Across filesVariables, functions, classesNeeds language support

My rule is simple enough. If the language knows the thing as a name, rename it, and if it’s plain text, replace it.

The Find widget is fast and contained, though it offers no diff preview. The Search view gives you a diff preview and file filters, and the price is a broad scope. Multi-cursor shows every edit as it happens but gets tedious as matches pile up. Rename Symbol follows the symbol instead of the text, as long as the language supports it.

Multi-cursor editing instead of replace

Ctrl+D (Command+D) selects the word at the cursor, or the next occurrence of the current selection. Ctrl+K Ctrl+D skips a match you don’t want. Ctrl+Shift+L (Shift+Command+L) goes the other way and selects every occurrence of the selected text at once.

For a handful of matches I would pick Ctrl+D over Replace All, because each edit appears on screen as you type.

One snag: a graphics driver can override the add-cursor shortcuts, Ctrl+Alt+Down for instance. Alt+Enter from the Find widget gets around it.

Rename Symbol for code identifiers

F2 renames the symbol, not the text.

Press F2, type the new name and press Enter, and all instances of the symbol across all files are renamed. Only some languages support renaming across files.

Microsoft’s Visual Studio documentation gives the same advice for its sibling IDE, noting that refactoring understands scope and find-and-replace changes every instance without looking at context.

Renaming through the language service is code refactoring, which is why it skips the look-alike text a replace would catch.

Use F2 when the target is a variable, function or class and the language supports renaming. The Search view is the better pick for a string, a comment, a config value, or code in a language without rename support.

Why Find and Replace Misses Matches or Changes the Wrong Text

Usually the culprit is a filter hiding files, or regex characters nobody escaped. Sometimes the engine handles the pattern differently from what you expected, and now and then an option from an earlier search is still switched on.

SymptomLikely causeFix
Hits missing from a file that has the termPath excluded or ignoredCheck the exclude box and the ignore-files toggle
Replace All changes too muchWhole word off, or unescaped regex charactersTurn on Match Whole Word, escape with a backslash
Nothing changes outside part of the fileFind in Selection is activeToggle it off with Alt+L
Lookahead pattern misbehaves across filesSearch view runs on ripgrepSimplify the pattern, test on a small folder
Alt+R does nothingAnother app holds the shortcutClick the toggle or rebind the key

The last row has a documented case: DevSense’s 2022 write-up reports that Alt+R failed for some users because it was bound to an NVIDIA stats-display toggle.

Unescaped characters in regex mode

With regex off, searching 1.5 finds exactly 1.5. Switch it on and the dot matches any character, so 1.5 also finds 135 and 1×5.

Escape it as 1.5. The same goes for $ ( ) [ ] { } + * ? ^ and |.

Pattern engines and lookarounds

A pattern can pass in one place and fail in another.

By default, VS Code uses ripgrep to drive search across files. The ripgrep FAQ states that its default regex engine does not support lookaround or backreferences, though ripgrep can optionally use PCRE2.

Community write-ups say VS Code falls back to PCRE2 when the default engine cannot run a pattern, which would let lookarounds work. That fallback is reported on DEV Community, and I found no statement of it in the VS Code docs.

Either way, test lookarounds on a small folder before a project-wide replace.

Slow searches and heavy results

Slow searches, or a pile of rg processes, usually mean the Use Exclude Settings and Ignore Files toggle is off. Disabled ignore files and large folders that aren’t excluded cause the same thing.

Large generated folders belong in search.exclude. **/node_modules/** and **/target/** are the obvious ones, and **/.worktrees/** goes in as well.

Check what changed before you commit

After a big replace, read the git diff output line by line.

If the diff is bigger than the replace you meant to make, discard the changes and redo the search with Match Whole Word on.

FAQ About Find and Replace in VSCode

What is find and replace in VSCode?

VS Code can locate matches in the open file or across a workspace and rewrite them, with case, whole-word and regular expression matching available. The Find widget covers one file, and the Search view covers the project.

Can I undo a Replace All in VSCode?

Yes, as long as the file is still open. Press Ctrl+Z (Command+Z on macOS) in that editor.

The VS Code docs I checked describe no project-wide undo, so commit or stash first and roll back through source control if a multi-file replace goes wrong.

How do I change the find and replace shortcuts?

Open the Keyboard Shortcuts editor with Ctrl+K Ctrl+S (Command+K Command+S on macOS) and search for the command, such as editor.action.startFindReplaceAction for Replace.

Right-clicking any action and choosing Configure Keybinding opens the editor already filtered to that command.

Can I turn off find and replace history?

Yes. VS Code saves find and replace history per workspace by default, and setting editor.find.history or editor.find.replaceHistory to never turns it off.

Past queries then stop coming back after a restart.

What to Check Before Replacing Across Many Files

Multi-file replaces in VSCode are safest on a clean working tree. Narrow the search first, and read the whole diff preview before you apply anything.

Order matters here. Commit or stash to start with, since a commit only protects work saved before the edit. Narrowing comes second, using include and exclude patterns plus Match Whole Word, because a smaller result set is a preview you can actually finish reading. Applying comes last.

It costs a few minutes per replace, which beats reverting changes file by file afterwards.

The VS Code documentation, checked in November 2026, describes no project-wide undo, which is why the clean tree leads. A built-in multi-file undo would change that order.

To check a result against an untouched copy of a file, compare two files in VSCode side by side.

Bogdan Sandu

Stay sharp. Ship better code.

Every week: one curated article, one tool worth knowing, one tip you can use tomorrow. No noise, no padding.