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

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

  1. Set the arglist with :args to define which files to edit:
    :args src/**/*.js
    
  2. Record your macro into register q:
    qq ... q
    
  3. Apply across all arglist files:
    :argdo execute 'normal @q' | update
    
  • :argdo {cmd} runs {cmd} for each file in the arglist, opening each in turn
  • execute 'normal @q' runs the macro from register q in Normal mode
  • update saves the file only if it was modified (equivalent to :write but no-op on unchanged files)
  • After completion, use :argdo diffthis or 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 :args to set it to exactly the files you need, not everything that happens to be open
  • :argdo opens each file, so it adds them to the buffer list; use :bdelete or :only to clean up afterward
  • For all open buffers instead of the arglist, use :bufdo execute 'normal @q' | update
  • Add silent! before execute to suppress per-file error messages if the macro pattern is not found in every file

Next

How do I run a search and replace only within a visually selected region?