Introduction
Find and Replace is one of VS Code's most powerful features. When replace stops working, you're stuck manually editing each match or using inefficient workarounds. This guide covers the common causes and fixes for replace failures.
Symptoms
- Replace button doesn't replace matches
- Replace replaces wrong content
- Regex replace produces unexpected results
- Replace preview shows changes but doesn't apply them
- Replace in files doesn't work
- Multi-file replace fails silently
Understanding Replace Modes
VS Code has several replace contexts:
- 1.Editor Find:
Ctrl+F- Replace within current file - 2.Search Panel:
Ctrl+Shift+F- Replace across all files - 3.Editor Search:
Ctrl+Shift+H- Replace in search editor
Each has slightly different behavior.
Step-by-Step Fixes
Step 1: Check Replace Mode
Ensure you're in replace mode:
In Editor Find (Ctrl+F), click the right arrow icon or press:
- Alt+R (Windows/Linux)
- Option+R (Mac)
Or use:
- Ctrl+H directly opens Find with Replace visible
In Search Panel (Ctrl+Shift+F):
- Replace field is always visible below the search field
- Enter replacement text in the second field
Step 2: Enable Replace Preview
Replace preview helps verify changes before applying:
// settings.json
"search.useReplacePreview": trueThis shows a diff view before actually replacing.
Step 3: Verify Match Selection
Replace only works on matches:
- 1.Ensure "Find" has returned matches (count shows in UI)
- 2.Check that matches are highlighted in the editor
- 3.Click a match or use navigation arrows
If no matches found, replace won't work.
Step 4: Apply Replace Correctly
Replace actions: - Replace Next: Replace one match, move to next - Replace All: Replace all matches at once - Replace in Files: Apply across multiple files
In Editor Find:
``
Replace: Alt+Enter (replace next)
Replace All: Ctrl+Alt+Enter
In Search Panel:
``
Replace: Click the replace icon on match
Replace All: Click "Replace All" button
Step 5: Fix Regex Replace Syntax
Regex replace requires proper syntax:
Capture Groups:
Use $1, $2, etc. to reference groups:
Find: function\s+(\w+)
Replace: const $1 = function
This transforms:
``javascript
function myFunc
// becomes
const myFunc = function
Named Groups:
Find: (?<name>\w+)
Replace: ${name}
Full Match:
Use $0 for the entire match.
Common Mistakes:
Wrong:
``
Find: \.
Replace: . // Missing escape
Correct:
``
Find: \.
Replace: .
Step 6: Handle Preserve Case
Preserve case matching replacement:
// settings.json
"editor.find.preserveCase": trueWhen enabled: - Finding "Test" and replacing with "Example" keeps case - "Test" becomes "Example" - "test" becomes "example" - "TEST" becomes "EXAMPLE"
Step 7: Fix Search Panel Replace
Search panel replace requires confirmation:
- 1.Enter find and replace patterns
- 2.Review matches in the list
- 3.Click replace icon on individual matches
- 4.Or click "Replace All" for all matches
If "Replace All" doesn't work:
// settings.json
"search.searchOnType": true,
"search.searchEditor.revealBehavior": "keepOpen"Step 8: Handle Multi-file Replace Issues
Multi-file replace can fail for several reasons:
Files excluded from search:
``json
// settings.json
"search.exclude": {}
Files not saved: Save all files before replacing:
File: Save AllOr enable auto save:
``json
"files.autoSave": "afterDelay"
Read-only files:
Check file permissions:
``bash
# Linux/macOS
chmod u+w filename
Step 9: Fix Large File Replace
Large files may fail to replace:
// settings.json
"files.maxMemoryForLargeFilesMB": 4096Step 10: Handle Search Editor
Search Editor (Ctrl+Shift+H) has replace:
// settings.json
"search.searchEditor.useGlobalSearch": true- 1.In Search Editor:
- 2.Type find pattern
- 3.Results appear in editor
- 4.Replace field is at top
- 5.Apply replaces directly
Step 11: Fix Undo After Replace
If you accidentally replace too much:
Edit: UndoOr Ctrl+Z
VS Code tracks replaces as edits, so you can undo.
Step 12: Handle Case Sensitivity
Replace respects case sensitivity:
Toggle case sensitivity (Aa icon) before replacing.
Case-sensitive replace: - Only replaces exact case matches - Won't replace "test" when searching for "Test"
Case-insensitive replace: - Replaces all case variants - Replaces "test", "Test", "TEST"
Step 13: Fix Word Match Replace
Word match affects replace scope:
Toggle word match (W icon) before replacing.
Word match replace: - Only replaces whole word matches - Won't replace "testing" when searching for "test"
Step 14: Handle Replace in Git Files
Git-controlled files need careful replace:
VS Code shows replace preview for modified files.
Check git status:
``bash
git status
Replace works on tracked files.
Step 15: Fix Replace History
VS Code remembers replace patterns:
Recent patterns are in the dropdown.
Clear history if corrupted:
Windows:
``powershell
rd /s /q "%APPDATA%\Code\User\History"
macOS:
``bash
rm -rf ~/Library/Application\ Support/Code/User/History
Linux:
``bash
rm -rf ~/.config/Code/User/History
Step 16: Handle Encoding in Replace
Non-UTF-8 files may have replace issues:
// settings.json
"files.encoding": "utf8",
"files.autoGuessEncoding": trueStep 17: Fix Performance Issues
Replace performance tuning:
// settings.json
"search.maxFiles": 20000,
"search.searchOnTypeDebouncePeriod": 300Large replace operations may take time.
Step 18: Extension Conflicts
Extensions can interfere with replace:
- 1.Open Extensions (
Ctrl+Shift+X) - 2.Search for "find", "replace", "search"
- 3.Disable suspicious extensions
- 4.Test replace
Verification
Test replace functionality:
// Test file with multiple occurrences
const test = "test value";
const test2 = "another test";
function test() {
return "test";
}Replace "test" with "example": - Should replace all 4 occurrences - Verify each replacement - Undo to verify restore
Test regex replace:
// Transform function declarations
function oldName() {}
function anotherOldName() {}Find: function\s+(\w+)\s*\(\)
Replace: const $1 = () =>
Result:
``javascript
const oldName = () => {}
const anotherOldName = () => {}
Common Replace Patterns
Add Text Before
Find: ^
Replace: // Comment:
Add Text After
Find: $
Replace: // end
Remove Text
Find: text.*
Replace: ``
Swap Groups
Find: (\w+)\s+(\w+)
Replace: $2 $1
Conditional Replace
Find: (?<=const\s)\w+
Replace: newName
Uses lookbehind to replace after "const".
Related Issues
- VS Code Search Not Working
- VS Code Multi-Cursor Not Working
- VS Code Regex Search Issues