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
- Load target files into the argument list:
:args **/*.txt - Record your macro into register
a:qa{your edits}q - Apply the macro to every file and save each one:
:argdo norm @a | update
:argdoruns the following command on each file in the argument list, visiting each in sequencenorm @areplays macroaon the current buffer| updatewrites 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
:argsalone 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:wato save any unsaved - For patterns across files,
:vimgrep /pat/ ** | cdo norm @a | updatetargets only matching lines