vimtricks.wiki Concise Vim tricks, one at a time.

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

  1. Populate the argument list with the files you want to edit:
:args **/*.go
  1. Confirm what's in the list:
:args
  1. 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 e flag (/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

  • :args accepts wildcards, globs, and backtick shell expansions: :args \git diff --name-only HEAD``
  • Use :argadd *.js and :argdelete unwanted.js to adjust the list before running
  • :argdo respects :set hidden—without it, Vim will refuse to move to the next file if the current one has unsaved changes; add set hidden to your vimrc or run :set hidden first
  • For a dry run, use :argdo %s/old/new/gn (the n flag counts matches without substituting)

Next

How do I scroll the view left and right when lines are wider than the screen?