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

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 | update to save changed files (:update only saves if modified)
  • Use :argdo execute 'silent! normal @q' | update for error-tolerant execution
  • Test the macro on one file first before running :argdo
  • :args shows the current file list, :argadd and :argdelete modify it
  • :bufdo operates on ALL open buffers — be careful if you have unrelated files open
  • Set :set hidden to allow switching buffers without saving first

Next

How do I run the same command across all windows, buffers, or tabs?