Editing Macros

In the executing macros section we identified a few issues with our macro:

  1. There are spaces between the tags and the text inside them, and

  2. We want to add a j at the end of the macro to automatically move the cursor down a line.

To start, lets hits G and Esc to move the cursor to the bottom of the buffer, then execute :put a to insert the macro we just recorded at the bottom of our buffer:

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
33%
2:33
 
Move to Bottom
G<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
83%
5:27
 
Insert Register
<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. 
o<\p>[kO<p>[JJ
NORMAL
86%
6:1
 

Lets take a moment to explain the text that was just inserted into the buffer. When we recorded the macro we used these steps:

o<\p><Esc>kO<p><Esc>JJ

but what gets pasted into the buffer is:

o<\p>^[kO<p>^[JJ

what is going on?

While it is convenient for humans to represent keys on by their names, such as <Esc>, terminals represent these keys internally as a sequence of bytes defined by ANSI escape codes. These appear in a terminal as a sequence of characters. For example, the bytes representing the escape key are 0x1b, which is rendered in the terminal as ^[, which explains why this is shown when we insert the macro contents into the buffer.

Note

If you look carefully, the text shown on this page is slightly different than what you see in your terminal:

o<\p>[kO<p>[JJ

The browser interprets the 0x1b byte sequence as text, and renders it as &#27;, which is a meaningless character. In order to keep this page content looking more similar to the terminal, we have replaced this character with a [.

Now, back to implementing our edits.

The first thing we want to do is remove the spaces between the tags and text. In the original macro, we used the J command to join lines together. When J joins lines it puts a space between them, which is usually what we would want, but not in this case. We should have used the alternate command gJ, which joins lines but does not add a space between them. We will start by moving the cursor to the first J using fJ, then add a g before it using i followed by g.

Next we want to move to the end of the line and add a g before the second J too. The final change will be to add a j to the end of the macro, so that the cursor moves down one line after executing. This is done by moving to the end of the line with $, then adding the j to the end using a j.

After the edits are made we want to store the updated macro in register a and delete it from the buffer. We can achieve this by switching to normal mode (<Esc>) then moving the cursor to the beginning of the line with 0. Finally, we want to delete text to the end of this line and put it into register a, using "ad$.

These steps are detailed below:

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.
o<\p>[kO<p>[JJ
NORMAL
86%
6:1
 
Move Cursor
fJ
<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.
o<\p>[kO<p>[JJ
NORMAL
86%
6:13
 
Add i
ig
<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.
o<\p>[kO<p>[gJJ
INSERT
86%
6:14
 
Move to end of 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.
o<\p>[kO<p>[gJJ
NORMAL
86%
6:15
 
Add i
ig
<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.
o<\p>[kO<p>[gJgJ
INSERT
86%
6:16
 
Add j
<Esc>$aj
<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.
o<\p>[kO<p>[gJgJj
INSERT
86%
6:18
 
Move to start of line
<Esc>0
<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.
o<\p>[kO<p>[gJgJj
NORMAL
86%
6:1
 
Delete to register a
"ad$
<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
86%
6:1
 

Finally, lets test the updated macro. Lets start by moving the cursor up to the second line with 4k, then execute the macro using @a,

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
86%
6:1
 
Move Cursor
4k
<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
29%
2:1
 
Execute macro
@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
43%
3:30
 
Execute macro (again)
@a
<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
57%
4:34
 
Execute macro (again)
@a
<p>·Beautiful·is·better·than·ugly.·<\p>
<p>Explicit·is·better·than·implicit.<\p>
<p>Simple·is·better·than·complex.<\p>
<p>Complex·is·better·than·complicated.<\p>
Flat·is·better·than·nested. 
 
NORMAL
71%
5:27
 
Execute macro (again)
@a
<p>·Beautiful·is·better·than·ugly.·<\p>
<p>Explicit·is·better·than·implicit.<\p>
<p>Simple·is·better·than·complex.<\p>
<p>Complex·is·better·than·complicated.<\p>
<p>Flat·is·better·than·nested.<\p>
 
NORMAL
86%
6:1
 

At this point our macro is working as expected. The only remaining task is to manually remove the spaces from line 1, and we are done.