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

How do I run a recorded macro on every file in my argument list at once?

Answer

:argdo normal @a

Explanation

The :argdo command applies any Ex command to every file in the argument list. Combined with normal @a, it replays a recorded macro across an entire set of files — making multi-file batch edits effortless without writing a script.

How it works

  • :argdo iterates through all files in the argument list (set via :args or files passed on startup)
  • normal @a executes the macro in register a as if typed in Normal mode
  • Vim opens each file, runs the macro, and advances to the next
  • Append | update to automatically save each modified file

Example

Suppose you want to strip trailing semicolons from every Go file:

" Set the argument list to all Go files
:args **/*.go

" Record a macro in register q
qq0$x:s/;$//e<CR>q

" Apply to all files and save
:argdo execute 'normal @q' | update

Tips

  • Run :set hidden first so Vim allows switching buffers with unsaved changes
  • Use :bufdo normal @a if the files are already open in buffers instead of the arg list
  • Prefer :argdo execute 'normal @a' over :argdo normal @a in scripts — execute properly surfaces errors per file
  • Combine with :vimgrep to build a targeted arg list: :vimgrep /TODO/ **/*.md | copen then :cfdo normal @a | update to act only on matched files

Next

How do I use capture groups in Vim substitutions to rearrange or swap matched text?