How do I apply a recorded macro to a specific set of files using the argument list?
Answer
:argdo execute 'normal @q' | update
Explanation
:argdo runs an Ex command on every file in Vim's argument list (the arglist). By combining it with execute 'normal @q', you apply a recorded macro to each file and then save the changes with update. This gives you precise, repeatable bulk edits across exactly the files you choose.
How it works
- Set the arglist with
:argsto define which files to edit::args src/**/*.js - Record your macro into register
q:qq ... q - Apply across all arglist files:
:argdo execute 'normal @q' | update
:argdo {cmd}runs{cmd}for each file in the arglist, opening each in turnexecute 'normal @q'runs the macro from registerqin Normal modeupdatesaves the file only if it was modified (equivalent to:writebut no-op on unchanged files)- After completion, use
:argdo diffthisor review diffs to verify the changes
Example
Remove a trailing console.log call from every .js file:
:args src/**/*.js
qq/console.log<CR>dd<Esc>q
:argdo execute 'normal @q' | update
Tips
- The arglist is independent of open buffers — use
:argsto set it to exactly the files you need, not everything that happens to be open :argdoopens each file, so it adds them to the buffer list; use:bdeleteor:onlyto clean up afterward- For all open buffers instead of the arglist, use
:bufdo execute 'normal @q' | update - Add
silent!beforeexecuteto suppress per-file error messages if the macro pattern is not found in every file