Macros are incredibly powerful
Macros are definitely up there amongst Vim’s most powerful features. The core problem that macros are trying to solve is that nobody likes making repetitive edits — it’s boring, error prone, and can take a very long time. Macros help us here by automating away routine edits, minimising legwork.
The great thing about them is that they’re conceptually very simple, and we don’t have to learn very many new concepts to start using them effectively.
Vim’s macro functionality lets us to record a sequence of keystrokes and replay them back at will. You can capture complex editing jobs with a macro, and replay it on demand to repeat the edit.
First, let’s learn how to record a macro. To start recording a macro, hit the q key in normal mode followed by a single letter identifier for the macro. For example, qq will start recording a macro called q, qa will start recording a macro called a — and so on. Once you do this, you’ll notice that Vim helpfully tells us that we’re recording a macro at the bottom left of the editor.
Once you’ve started recording, start editing. Your keystrokes will be stored inside the macro and you’ll be able to replay them later.
Once you’ve finished with your edit, you can hit q again to stop recording.
Let’s try it in the editor — start recording a macro with qa, make a few edits, and hit q again to stop recording. We’ll learn how to replay this macro in the next section.