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

How do I add more commands to a macro I already recorded?

Answer

qA

Explanation

If you finish recording a macro and realize you forgot a step, you don't need to re-record the whole thing. Using an uppercase register letter with q appends new keystrokes to an existing macro instead of overwriting it. So qA appends to the macro in register a.

How it works

  • qa starts recording a new macro into register a, replacing any previous contents
  • qA (uppercase) starts recording and appends new keystrokes to whatever is already in register a
  • Press q again to stop recording
  • When you replay with @a, Vim executes the original keystrokes followed by the appended ones as a single seamless macro

Example

You record a macro that wraps a word in parentheses:

qa bi(<Esc> ea)<Esc> q

Register a now contains bi(<Esc>ea)<Esc>. But you realize you also want to move to the next line afterward. Instead of re-recording everything:

qA j q

Register a now contains bi(<Esc>ea)<Esc>j. The j was appended seamlessly.

Given the text:

alpha
beta
gamma

Running 3@a from the first word produces:

(alpha)
(beta)
(gamma)

Tips

  • This works for all 26 named registers: qB appends to register b, qC to register c, and so on
  • Use :reg a to inspect the current contents of register a before and after appending
  • If the appended portion has a mistake, you can edit the register directly: paste it with "ap, fix the text, then yank it back with "ayy
  • Appending is especially useful for building up complex macros incrementally — record the basic operation first, test it, then append refinements
  • Remember that macro registers and yank registers are the same registers — appending with qA also appends to the yank register a, and vice versa with "Ayy
  • Combine with recursive macros: record qa ... q, test it, then append the recursive call with qA @a q

Next

How do I edit multiple lines at once using multiple cursors in Vim?