Executing Macros

After a macro is recorded it is stored in a register, then it can be executed using the pattern:

@{name}

where name is the name of the register that holds the macro. Continuing with our example from the previous section, we have recorded out macro and we have stored it in the a register, and our buffer looks like this:

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:35
 

Now, let's move the cursor to the next line and execute our macro:

Move down one line and execute the macro
j@a
<p>·Beautiful·is·better·than·ugly.·<\p>
<p>·Explicit·is·better·than·implicit.·<\p>
Simple·is·better·than·complex.
Complex·is·better·than·complicated.
Flat·is·better·than·nested.
NORMAL
33%
2:38
 

Excellent! Our macro appears to be working well, so let's proceed with executing it on a few more lines. This time, however, let's use a shortcut - after a macro is executed using @{name} we can execute it again using @@, which saves us from moving our hands across the keyboard and allows macros to be repeated quickly.

Let's try:

Move down one line and execute the macro again
j@@
<p>·Beautiful·is·better·than·ugly.·<\p>
<p>·Explicit·is·better·than·implicit.·<\p>
<p>·Simple·is·better·than·complex.·<\p>
Complex·is·better·than·complicated.
Flat·is·better·than·nested.
NORMAL
50%
3:35
 

Very handy, but better still - Macros also support counts, allowing them to be automatically executed multiple times in a row, using:

{count}@{name}

Let's give that a try:

Execute the macro while specifying a count
2@a
<p>·Beautiful·is·better·than·ugly.·<\p>
<p>·Explicit·is·better·than·implicit.·<\p>
<p>·<p>·<p>·Simple·is·better·than·complex.·<\p>·<\p>·<\p>
Complex·is·better·than·complicated.
Flat·is·better·than·nested.
NORMAL
50%
3:53
 

Wait, that didn't work as expected. What happened?

When macros are executed it is important that the cursor be placed in the correct location prior to executing them. This is why when we executed our macro the first time we first moved the cursor down a row using j, then we executed the macro. However, when we execute macros with counts we don't have an opportunity to move the cursor between invocations. For this reason, it is often good practice to reposition the cursor in the macro, so that the macro is compatible with counts.

So, although our macro is executing, we need to make a few changes to position the cursor correctly. Also, as we executed the macro you might have noticed one other item that was overlooked when the macro was recorded - there are unnecessary spaces between the text and the HTML tags. So, that gives us two things that we need to fix in our macro.

Lets undo the changes that we made in this section:

Undo
3u
<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
33%
2:33
24 changes; before #5 0 seconds ago

In the next section we will take a look at how we can edit macros after they have been recorded in order to fix them without having to re-record them from scratch.