Practical Vim< /a> vit Practical Vim< /a> Note that the styling for a Visual selection is the... development I thought I knew Vim, but Practical" name="description"/>
Tải bản đầy đủ - 0 (trang)
Tip 67. Repeat a Change on Contiguous Lines

Tip 67. Repeat a Change on Contiguous Lines

Tải bản đầy đủ - 0trang

Repeat a Change on Contiguous Lines



1)

2)

3)

4)



• 165



One

Two

Three

Four



The task may look trivial, but it presents a couple of interesting challenges.



Record One Unit of Work

To begin, we record all changes made to the first line:

Keystrokes



Buffer Contents



qa



1. one

2. two



0f.



1. one

2. two



r)



1) one

2. two



w~



1) One

2. two



j



1) One

2. two



q



1) One

2. two



Note the use of motions in this macro. We begin with the 0 command, which

normalizes our cursor position by placing it at the start of the line. This means

that our next motion always starts from the same place, making it more

repeatable.

Some might look at the next motion, f. , and consider it wasteful. It moves

the cursor only one step to the right, same as the l command. Why use two

keystrokes when one would do?

Once again, it’s a matter of repeatability. In our sample set, we have lines

numbered only one to four, but suppose the numbers ran into double digits?

1. one

2. two

...

10. ten

11. eleven



On the first nine lines, 0l takes us to the second character of the line, which

happens to be a period. But from line ten onward, that motion stops short of



www.it-ebooks.info



report erratum • discuss



Chapter 11. Macros



• 166



the target, whereas f. works on all of these lines and would continue to work

into triple digits and beyond.

Using the f. motion also adds a safety catch. If no . characters are found on

the current line, the f. command raises an error and macro execution aborts.

We’ll exploit this later, so keep that thought at the back of your mind.



Execute Macro in Series

We can execute the macro we just recorded by pressing @a . This carries out

the following steps: jump to the first . character on the line, change it to ),

uppercase the first letter of the next word, and finish by advancing to the

next line.

We could invoke the @a command three times to complete the task, but running 3@a is quicker:

Keystrokes



Buffer Contents



{start}



1)

2.

3.

4.



One

two

three

four



3@a



1)

2)

3)

4)



One

Two

Three

Four



Let’s introduce a new obstacle. Suppose that the lines we want to act upon

are broken into chunks by the occasional comment:

macros/broken-lines.txt

1. one

2. two

// break up the monotony

3. three

4. four



Now watch what happens if we attempt to replay the same macro on this file

(Table 17, Replaying the Macro, on page 167).

The macro stalls on line three—the one containing the comment. When the

f. command is executed, it finds no . characters and the macro aborts. We’ve

tripped the safety catch, and it’s a good thing too. If the macro had successfully executed on this line, then it would have made changes that were

probably unwanted.



www.it-ebooks.info



report erratum • discuss



Repeat a Change on Contiguous Lines



Keystrokes



Buffer Contents



{start}



1.

2.

//

3.

4.



one

two

break up the monotony

three

four



5@a



1)

2)

//

3.

4.



One

Two

break up the monotony

three

four



• 167



Table 17—Replaying the Macro

But we are left with a problem. We asked Vim to execute the macro five times,

and it bailed out on the third repetition. So we have to invoke it again on the

next lines to complete the job. Let’s look at an alternative technique.



Execute Macro in Parallel

Tip 30, on page 61, demonstrated a method for running the dot command on

a series of consecutive lines. We can apply the same technique here:

Keystrokes



Buffer Contents



qa



1. one



0f.r)w~



1) One



q



1) One



jVG



1)

2.

//

3.

4.



One

two

break up the monotony

three

four



:'<,'>normal @a



1)

2)

//

3)

4)



One

Two

break up the monotony

Three

Four



We’ve re-recorded the macro from scratch. This one is almost identical, except

that we’ve omitted the final j command to advance to the next line. We won’t

be needing it this time.

The :normal @a command tells Vim to execute the macro once for each line in

the selection. Just as before, the macro succeeds on the first two lines and



www.it-ebooks.info



report erratum • discuss



Chapter 11. Macros



• 168



then aborts on line three, but it doesn’t stall there this time—it completes the

job. Why?

Previously, we queued up five repetitions in series by running 5@a . When the

third iteration aborted, it killed the remaining items in the queue. This time,

we’ve lined up five iterations in parallel. Each invocation of the macro is

independent from the others. So when the third iteration fails, it does so in

isolation.



Deciding: Series or Parallel

Which is better, series or parallel? The answer (as always): it depends.

Executing a macro on multiple items in parallel is more robust. In this scenario, it’s the better solution. But if we raise an error when we execute a

macro, maybe we want those alarms to go off. Executing a macro on multiple

items in series makes it clear when and where any errors occur.

Learn both techniques, and you’ll develop a knack for knowing which one is

right for the occasion.



Tip 68



Append Commands to a Macro

Sometimes we miss a vital step when we record a macro. There’s no need to

re-record the whole thing from scratch. Instead, we can tack extra commands

onto the end of an existing macro.

Suppose that we record this macro (borrowed from Tip 67, on page 164):

Keystrokes



Buffer contents



qa



1. one

2. two



0f.r)w~



1) One

2. two



q



1) One

2. two



Immediately after pressing q to stop recording, we realize that we should have

finished by pressing j to advance to the next line.

Before we fix it, let’s inspect the contents of register a:



www.it-ebooks.info



report erratum • discuss



Act Upon a Collection of Files









:reg a

"a



• 169



0f.r)w~



If we type qa , then Vim will record our keystrokes, saving them into register

a by overwriting the existing contents of that register. If we type qA , then Vim

will record our keystrokes, appending them to the existing contents of register

a. We can amend our mistake:

Keystrokes



Buffer Contents



qA



1) One

2. two



j



1) One

2. two



q



1) One

2. two



Let’s see what’s in the a register now:









:reg a

"a



0f.r)w~j



All of the commands that we recorded the first time around are still there,

but now it ends with j.



Discussion

This little trick saves us from having to re-record the entire macro from

scratch. But we can use it only to tack commands on at the end of a macro.

If we wanted to add something at the beginning or somewhere in the middle

of a macro, this technique would be of no use to us. In Tip 71, on page 176,

we’ll learn about a more powerful method for amending a macro after it has

been recorded.



Tip 69



Act Upon a Collection of Files

So far, we’ve stuck to tasks that were repeated in the same file, but we can

play back a macro across a collection of files. Once again, we’ll consider how

to execute the macro in parallel and in series.

Let’s start with a set of files that look something like this:



www.it-ebooks.info



report erratum • discuss



Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Tip 67. Repeat a Change on Contiguous Lines

Tải bản đầy đủ ngay(0 tr)

×