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

How do I record a macro in Vim?

Answer

q{a-z}...q

Explanation

Recording a macro captures a sequence of keystrokes into a register, which you can replay later. Press q followed by a register letter to start, perform your actions, then press q again to stop.

How it works

  • qa starts recording into register a
  • Perform any sequence of Vim commands
  • q stops recording
  • @a replays the macro

Example

To wrap each line in <li> tags:

  1. qa — start recording
  2. I<li><Esc>A</li><Esc>j — add tags and move down
  3. q — stop recording
  4. @a — replay on next line

Tips

  • @@ replays the last macro
  • 5@a runs macro a five times
  • The recording indicator appears in the statusline
  • All 26 registers (a-z) can store macros
  • Macros and registers share the same storage

Next

How do you yank a single word into a named register?