Vim Scoops / Lessons / Record a macro

How to record a macro in Vim

Press qa to start recording into register a, make your edit, then press q to stop. @a plays it back, and @@ repeats the last one.

The keys

KeysWhat they do
qa … qRecord into register a
@aPlay it back
10@aPlay it ten times
@@Repeat the last macro you played

A macro is just the keys you pressed, played back. The trick is to finish where the next run should start, usually with j to drop to the next line.

Start with 0 or ^ so the macro doesn't depend on where the cursor happened to be. If it fails partway, it stops. That makes a big count safe.

Related

Want all of Vim, in order? Vim Scoops is a free game that starts at h j k l and builds up to macros.

Play the full game