Recording Macros

The first step to creating a macro is to plan the individual steps that will be recorded. After the steps have been planned, it is time to record the macro.

To start recording a macro, simply type q followed by the name of the named register that you would like to store the macro in. After that, proceed through the sequence of steps that you would like to record, then when you are done simply type q again to stop recording and save the macro in the register.

Lets walk through an example. Suppose we want to create a macro that wraps each line of text in HTML <p> tags. We can approach this in a few different ways, but for this example we will follow these steps:

  1. Start recording the macro
  2. Insert a line below the current line and add the closing tag
  3. Insert a line above the current line and add the opening tag
  4. Join all three lines together
  5. Stop recording the macro

Now, let's break each of these steps down into the sequence of keystrokes required to implement it, and record them. The keystrokes for Step 1 are:

  1. Press q to begin recording the macro, then a to specify which register to store it in
  2. Press o to insert a line below the current line and enter insert mode
  3. Enter the text to create the closing tag
  4. Press Esc to return to normal mode

Here is that sequence of steps:

Initial Conditions
Beautiful·is·better·than·ugly.
Explicit·is·better·than·implicit.
Simple·is·better·than·complex.
Complex·is·better·than·complicated.
Flat·is·better·than·nested.
NORMAL
Top
1:1
 
Type qa to starting recording the macro
qa
Beautiful·is·better·than·ugly.
Explicit·is·better·than·implicit.
Simple·is·better·than·complex.
Complex·is·better·than·complicated.
Flat·is·better·than·nested.
NORMAL
Top
1:1
 
Insert line below and enter INSERT mode
o
Beautiful·is·better·than·ugly.
 
Explicit·is·better·than·implicit.
Simple·is·better·than·complex.
Complex·is·better·than·complicated.
Flat·is·better·than·nested.
INSERT
29%
2:1
 
Enter the text for the closing tag
<\p>
Beautiful·is·better·than·ugly.
<\p>
Explicit·is·better·than·implicit.
Simple·is·better·than·complex.
Complex·is·better·than·complicated.
Flat·is·better·than·nested.
INSERT
29%
2:5
 
Enter NORMAL mode
<Esc>
Beautiful·is·better·than·ugly.
<\p>
Explicit·is·better·than·implicit.
Simple·is·better·than·complex.
Complex·is·better·than·complicated.
Flat·is·better·than·nested.
NORMAL
29%
2:4
 

So far so good. Now, let's create the opening tag. The keystrokes required for Step 2 are:

  1. Press O to insert a line above the current line and enter insert mode
  2. Enter the text to create the opening tag
  3. Press Esc to return to normal mode.

Let's watch that sequence of steps:

Initial Conditions
Beautiful·is·better·than·ugly.
<\p> 
Explicit·is·better·than·implicit.
Simple·is·better·than·complex.
Complex·is·better·than·complicated.
Flat·is·better·than·nested.
NORMAL
29%
2:4
 
Insert line above and enter INSERT mode
k
Beautiful·is·better·than·ugly.
<\p> 
Explicit·is·better·than·implicit.
Simple·is·better·than·complex.
Complex·is·better·than·complicated.
Flat·is·better·than·nested.
NORMAL
Top
1:4
 
Insert opening tag
O
 
Beautiful·is·better·than·ugly.
<\p>
Explicit·is·better·than·implicit.
Simple·is·better·than·complex.
Complex·is·better·than·complicated.
Flat·is·better·than·nested.
INSERT
Top
1:1
 
Enter NORMAL mode
<p>
<p>
Beautiful·is·better·than·ugly.
<\p>
Explicit·is·better·than·implicit.
Simple·is·better·than·complex.
Complex·is·better·than·complicated.
Flat·is·better·than·nested.
INSERT
Top
1:4
 
Join next line with the current line
<Esc>
<p>
Beautiful·is·better·than·ugly.
<\p>
Explicit·is·better·than·implicit.
Simple·is·better·than·complex.
Complex·is·better·than·complicated.
Flat·is·better·than·nested.
NORMAL
Top
1:3
 

Finally, now we only need to join all three lines together by executing J twice:

  1. Press J to join the opening tag with the text.
  2. Press J a second time, to join the closing tag with the text.
  3. Press q to stop recording
Initial Conditions
<p> 
Beautiful·is·better·than·ugly.
<\p>
Explicit·is·better·than·implicit.
Simple·is·better·than·complex.
Complex·is·better·than·complicated.
Flat·is·better·than·nested.
NORMAL
Top
1:3
 
Join next line with the current line (again)
J
<p>·Beautiful·is·better·than·ugly.
<\p>
Explicit·is·better·than·implicit.
Simple·is·better·than·complex.
Complex·is·better·than·complicated.
Flat·is·better·than·nested.
NORMAL
Top
1:4
 
Join next line with the current line (again)
J
<p>·Beautiful·is·better·than·ugly.·<\p>
Explicit·is·better·than·implicit.
Simple·is·better·than·complex.
Complex·is·better·than·complicated.
Flat·is·better·than·nested.
NORMAL
Top
1:35
 
Press q to stop recording
q
<p>·Beautiful·is·better·than·ugly.·<\p>
Explicit·is·better·than·implicit.
Simple·is·better·than·complex.
Complex·is·better·than·complicated.
Flat·is·better·than·nested.
NORMAL
Top
1:35
 

Great, we have recorded our macro. In the next section we will take a look at how to execute it.