How do I apply a macro across multiple files at once?
Answer
:argdo normal @q | update
Explanation
The :argdo command runs a command in every file in the argument list. Combined with normal @q, it executes your recorded macro across all files — a powerful batch-editing workflow.
Full workflow
" 1. Open target files
:args **/*.js
" 2. Record macro on one file
qq {edit commands} q
" 3. Apply to all files and save
:argdo execute 'normal @q' | update
Step-by-step example: Add license header
" 1. Load all Python files
:args **/*.py
" 2. Record macro to add header
qqggO# Copyright 2025 MyCompany<Esc>o# Licensed under MIT<Esc>o<Esc>q
" 3. Apply to all files
:argdo normal @q | update
Other do-commands
:argdo normal @q | update " Argument list files
:bufdo normal @q | update " All open buffers
:windo normal @q " All visible windows
:tabdo normal @q " All tabs
:cdo normal @q | update " Each quickfix entry
:cfdo normal @q | update " Each quickfix file
Tips
- Always append
| updateto save changed files (:updateonly saves if modified) - Use
:argdo execute 'silent! normal @q' | updatefor error-tolerant execution - Test the macro on one file first before running
:argdo :argsshows the current file list,:argaddand:argdeletemodify it:bufdooperates on ALL open buffers — be careful if you have unrelated files open- Set
:set hiddento allow switching buffers without saving first