How do I run a substitution across the arglist and write only modified files?
Answer
:argdo %s/foo/bar/ge | update
Explanation
Bulk replacements across many files are risky when every buffer gets written unconditionally. This :argdo pattern applies one substitution per argument-list entry, suppresses no-match errors, and writes only files that actually changed. It keeps refactors fast while avoiding noisy timestamp churn in unchanged files.
How it works
:argdoexecutes a command in each file currently in the argument list%s/foo/bar/geperforms a global substitute in each filegreplaces all matches per lineesuppresses errors for files with no matches| updatewrites only when the buffer is modified
This is ideal for targeted rename waves after populating the arglist with a curated file set.
Example
Suppose your arglist contains three files and only two include foo.
Run:
:argdo %s/foo/bar/ge | update
Result:
- matching files are edited and saved
- non-matching files are left untouched
- no pattern-not-found interruptions
Tips
- Build the arglist first with
:args **/*.py(or a narrower glob). - Add
c(.../gc) for confirmation when replacements are high risk.