Step 5 of 5

17. Macros

Appending to macros

Sometimes, you’ll record a macro and then think to yourself “damn — I forgot to do this small thing at the end”. If this happens, you don’t have to re-record the entire macro. You can append additional steps to the macro by recording to the uppercase register where it’s stored!

So, if your macro was originally recorded with qa, you can re-enter recording mode but append to the macro that you recorded previously with qA.

This is useful for the common situation where you only need to make a small change at the end of the macro’s sequence of actions.

Try it in the editor. Work through the checklist below. Record a macro into the "a register that prefixes the line with XXX, and replay it on the second line. Then, append to the macro with qA — this time adding XXX to the end of the line — and replay it once more on the last line. You’ll see that the final replay includes the steps from both of the recording sessions. Nice!

  1. Start recording into a Press qa — Vim will show recording @a in the bottom-left.
  2. Prefix the line with XXX Press I, type XXX, and press Esc.
  3. Stop recording Press q.
  4. Replay it on the second line Press j to drop to the second line, then @a.
  5. Resume recording with qA The uppercase name means "keep what's in a and record more" — press qA.
  6. Add XXX to the end of the line Press A, type XXX, and press Esc.
  7. Stop recording again Press q.
  8. Replay the combined macro on the last line Press j, then @a — both recordings play back in one go.
Start recording into a Press qa — Vim will show recording @a in the bottom-left.

Next: Prefix the line with XXX

Loading editor…