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

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

  • :argdo executes a command in each file currently in the argument list
  • %s/foo/bar/ge performs a global substitute in each file
  • g replaces all matches per line
  • e suppresses errors for files with no matches
  • | update writes 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.

Next

How do I tune Vim diff mode for more readable side-by-side hunks?