How do I run a substitution across multiple files using Vim's argument list?
Answer
:argdo %s/old/new/g | w
Explanation
:argdo {cmd} executes an Ex command against every file in the argument list—the set of files you opened Vim with or set explicitly with :args. Combined with :w (or :update to write only if changed), it provides precise, targeted batch editing across a known file set—more predictable than :bufdo, which also touches scratch buffers and help files.
How it works
- Populate the argument list with the files you want to edit:
:args **/*.go
- Confirm what's in the list:
:args
- Run the batch substitution and save each file:
:argdo %s/OldName/NewName/g | update
%s/old/new/g— substitute across the whole buffer| update— write the file only if it was modified (safer than| w)- Add
eflag (/ge) to suppress "pattern not found" errors for files where the match doesn't exist
Example
Rename a Go package identifier across all .go files:
:args **/*.go
:argdo %s/\<UserID\>/AccountID/ge | update
The \< and \> word boundaries ensure only exact matches are replaced, not OldUserID or similar.
Tips
:argsaccepts wildcards, globs, and backtick shell expansions::args \git diff --name-only HEAD``- Use
:argadd *.jsand:argdelete unwanted.jsto adjust the list before running :argdorespects:set hidden—without it, Vim will refuse to move to the next file if the current one has unsaved changes; addset hiddento your vimrc or run:set hiddenfirst- For a dry run, use
:argdo %s/old/new/gn(thenflag counts matches without substituting)