How do I run a search-and-replace across all files in my argument list and only save changed buffers?
Answer
:argdo %s/\<old\>/new/ge | update
Explanation
When you need to apply the same substitution across a curated set of files, :argdo is safer than a broad project-wide command. It only touches files currently in Vim's argument list, so you can control scope up front (for example, :args src/**/*.lua). Pairing substitution with update writes only modified buffers, which keeps your run fast and avoids unnecessary file timestamp churn.
How it works
:argdoexecutes the following Ex command once per file in the argument list%s/\<old\>/new/geruns substitution across the full buffer in each file\<and\>anchor a whole word match (oldbut notolder)greplaces all matches per lineesuppresses "pattern not found" errors so the loop continues cleanly| updatewrites the buffer only if it changed
Example
Start with argument list files containing:
# file a.txt
old value
keep old
# file b.txt
unchanged
Run:
:argdo %s/\<old\>/new/ge | update
Result:
# file a.txt
new value
keep new
# file b.txt
unchanged
Tips
- Build scope first with
:args, then inspect with:argsagain before running:argdo - Add
cto confirm each replacement interactively when risk is high - Use
:argdo vimgrep /pattern/ %first if you want evidence before modifying