Tip 71Evaluate an Iterator to Number Items in a List

Being able to insert a value that changes for each execution of a macro can be useful. In this tip, we’ll learn a technique for incrementing a number as we record a macro so that we can insert the numbers 1 to 5 on consecutive lines.

Suppose that we want to create a numbered list from a series of items on adjacent lines. To demonstrate, we’ll start with this text:

 partridge in a pear tree
 turtle doves
 French hens
 calling birds
 golden rings

We’ll transform it to look like this:

 1) partridge in a pear tree
 2) turtle doves
 3) French hens
 4) calling birds
 5) golden rings

We’ve already learned a couple of ways to make Vim perform simple arithmetic. We can ...

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.