Macro for making numbered lists in vim?

后端 未结 7 1955
渐次进展
渐次进展 2021-02-12 03:40

Often times it seems I have a list of items, and I need to add numbers in front of them. For example:

Item one
Item two
Item three

Which shoul

7条回答
  •  渐次进展
    2021-02-12 04:36

    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
      # copy the first few characters from the line above  
                         # back to normal mode
    |                         # go back to the start of the line
                      # 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.

提交回复
热议问题