Tip 68Repeat a Change on Contiguous Lines

We can make light work out of repeating the same set of changes on a range of lines by recording a macro and then playing it back on each line. There are two ways to do this: executing the macro in series or in parallel.

As a demonstration, we’ll transform this snippet of text:

 1. one
 2. two
 3. three
 4. four

We’ll make it look like this:

 1) One
 2) Two
 3) Three
 4) 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:

KeystrokesBuffer 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 ...

Get Practical Vim, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.