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

How do I run a recorded macro across multiple files at once?

Answer

:argdo norm @a | update

Explanation

Combining :argdo with :norm @a lets you apply a recorded macro to every file in Vim's argument list — a powerful pattern for bulk refactoring across a project without leaving the editor.

How it works

  1. Load target files into the argument list:
    :args **/*.txt
    
  2. Record your macro into register a:
    qa{your edits}q
    
  3. Apply the macro to every file and save each one:
    :argdo norm @a | update
    
  • :argdo runs the following command on each file in the argument list, visiting each in sequence
  • norm @a replays macro a on the current buffer
  • | update writes the file only if it was modified (safer than | w)

Example

Suppose you want to append a semicolon to the first line of every .js file in your project:

" Record: go to line 1, end of line, insert semicolon
qa1G$a;<Esc>q

" Apply to all .js files:
:args **/*.js
:argdo norm @a | update

Tips

  • Use :args alone to inspect the current file list before running
  • Combine with silent! to skip files where the macro fails: :argdo silent! norm @a | update
  • After :argdo, all modified files are in the buffer list — use :wa to save any unsaved
  • For patterns across files, :vimgrep /pat/ ** | cdo norm @a | update targets only matching lines

Next

How do I run text I've yanked or typed as a Vim macro without recording it first?