Macro for making numbered lists in vim?

You can easily record a macro to do it.

First insert 1. at the start of the first line (there are a couple of spaces after the 1. but you can't see them).

Go to the start of the second line and go into record mode with qa.

Press the following key sequence:

i                         # insert mode
<ctrl-Y><ctrl-Y><ctrl-Y>  # copy the first few characters from the line above  
<ESC>                     # back to normal mode
|                         # go back to the start of the line
<ctrl-A>                  # increment the number
j                         # down to the next line
q                         # stop recording

Now you can play back the recording with @a (the first time; for subsequent times, you can do @@ to repeat the last-executed macro) and it will add a new incremented number to the start of each line.


Select your lines in visual mode with: V, then type:

:'<,'>s/^\s*\zs/\=(line('.') - line("'<")+1).'. '

Which is easy to put in a command:

command! -nargs=0 -range=% Number <line1>,<line2>s/^\s*\zs/\=(line('.') - <line1>+1).'. '

Tags:

Vim

Macros