Step 2 of 6

Replaying macros

Replaying macros

Once you’ve recorded a macro, you can replay it by pressing q (lowercase) in normal mode. The pair is easy to remember: Q records, q plays.

It’s easier to see the magic of macros in practice, so let’s try this in the editor. Work through the checklist below: record a macro that puts an X at the start and the end of the cursor line, then move to the second line and replay the whole edit with q.

Wow! Did you see that? The macro did all the hard work for us!

One thing to keep in mind: a replay starts from wherever your cursor happens to be, with whatever selection you happen to have. I and A don’t care about that, which is why this macro works on any line — but a macro that starts with e or w will behave differently depending on where you were standing. Starting a recording with gh (go to line start) is a cheap way to make a macro line-proof.

Now that we know the basics of how to use macros, we’ll delve into them in a bit more detail in the coming sections.

  1. Put your cursor on the first line Jump to the top of the buffer — press gg.
  2. Start recording Press Q — the [@] flag appears in the bottom-right.
  3. Prefix the line with an X Press IX, then Esc to return to normal mode.
  4. Append an X as well Press AX, then Esc again.
  5. Stop recording Press Q — the [@] flag disappears.
  6. Move to the second line Press j.
  7. Replay the macro Press q and watch both edits happen in one go.
Put your cursor on the first line Jump to the top of the buffer — press gg.

Next: Start recording

Loading editor…