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:
Now, let's move the cursor to the next line and execute our macro:
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:
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:
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:
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.