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 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.
Lets walk through an example. Starting with the buffer in the first tab below, suppose we want to
wrap each line in HTML "p" tags. This can be approached in a few ways, but lets try it this way:
first we will use o to insert a line below the current line and enter insert mode, then we will
type the text for the closing tag, then finally return to normal mode.
Lets watch each step:
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
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
Insert 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, for the opening tag we will use O to insert a line above the current line and
enter insert mode, then type the opening tag, and finally return to normal mode. Here are those
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
Move up one line
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 line above and enter INSERT mode
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
Insert opening tag
<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
Enter NORMAL mode
<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, we need to join the join the lines by executing J twice:
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
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
These steps achieve our goal, so it is time to record the macro.
We want to store this macro in the aregister, so hit qa to start recording, repeat the steps,
then type q to stop recording.