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

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

  • :argdo executes the following Ex command once per file in the argument list
  • %s/\<old\>/new/ge runs substitution across the full buffer in each file
  • \< and \> anchor a whole word match (old but not older)
  • g replaces all matches per line
  • e suppresses "pattern not found" errors so the loop continues cleanly
  • | update writes 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 :args again before running :argdo
  • Add c to confirm each replacement interactively when risk is high
  • Use :argdo vimgrep /pattern/ % first if you want evidence before modifying

Next

How do I keep a utility split from resizing when other windows open or close?