Advanced Search and Replace Across Files
Master Vim's search and replace with regex, multi-file substitution, and confirmation workflows for safe bulk edits.
Prerequisites
- -Neovim or Vim installed
Steps
Basic search and replace in current file
Replace all occurrences of a pattern in the entire file.
Add 'c' flag (:%s/old/new/gc) to confirm each replacement individually.
Replace with regex capture groups
Use capture groups to restructure text during replacement.
This example swaps two words separated by an underscore: foo_bar becomes bar_foo.
Replace only in a visual selection
Select a range of lines visually, then substitute only within that range.
Search and replace across multiple files with argdo
Open files matching a pattern and run substitution across all of them.
Always use the 'e' flag with argdo to suppress errors on files that do not contain the pattern.
Use cfdo for quickfix-list-based replacement
Populate the quickfix list with grep results, then replace across those matches.
This is more targeted than argdo because it only edits files that contain the match.
Full Script
FAQ
Discussion
Loading comments...