7

I have a source document with the following text

Here is a bunch of text
...
Collect underpants
???
Profit!
...
More text

I would like to visually select the middle three lines and insert numbers in front of them:

Here is a bunch of text
...
1. Collect underpants
2. ???
3. Profit!
...
More text

All the solutions I found either put the numbers on their own new lines or prepended the actual line of the file.

How can I prepend a range of numbers to existing lines, starting with 1?

user1717828
  • 7,122
  • 8
  • 34
  • 59
  • 2
    Reread [this answer](http://stackoverflow.com/a/253041/1890567) from the second question you posted and explain how that doesn't answer your question. – FDinoff Aug 17 '15 at 14:45

4 Answers4

12

It makes for a good macro.

  1. Add the first number to your line, and put your cursor back at the beginning.
  2. Start a macro with qq (or q<any letter>)
  3. Copy the number with yf<space> (yank find )
  4. Move down a line with j
  5. Paste your yank with P
  6. Move back to the beginning of the line with 0
  7. Increment the number with Ctrl-a
  8. Back to the beginning again with 0 (incrementing positions you at the end of the number)
  9. End the macro by typing q again
  10. Play the macro with @q (or @<the letter you picked>)
  11. Replay the macro as many times as you want with <number>@@ (@@ replays the last macro)
  12. Profit!

To summarize the fun way, this GIF image is i1. <Esc>0qqyf jP0^a0q10@q. Vim list macro

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Kristján
  • 18,165
  • 5
  • 50
  • 62
4

To apply enumeration for all lines:

:let i=1 | g/^/s//\=i.'. '/ | let i=i+1

To enumerate only selected lines:

:let i=1 | '<,'>g/^/s//\=i.'. '/ | let i=i+1
DmitrySandalov
  • 3,879
  • 3
  • 23
  • 17
  • Could you explain what the top commands is doing? I get that `i` is a counter and `g` is matching beginning of lines, but not anything else. – user1717828 Feb 05 '20 at 11:06
3

Set non recursive mapping with following command and type ,enum in command mode when cursor is inside the lines you are going to enumerate.

    :nn ,enum {j<C-v>}kI0. <Esc>vipg<C-a>

TL;DR

You can type :help CTRL-A to see an answer on your question.

{Visual}g CTRL-A        Add [count] to the number or alphabetic character in
                        the highlighted text. If several lines are
                        highlighted, each one will be incremented by an
                        additional [count] (so effectively creating a
                        [count] incrementing sequence).
                        For Example, if you have this list of numbers:
                                1. 
                                1. 
                                1. 
                                1. 
                        Move to the second "1." and Visually select three
                        lines, pressing g CTRL-A results in:
                                1. 
                                2. 
                                3. 
                                4. 

If you have a paragraph (:help paragraph) you can select it (look at :help object-select). Suppose each new line in the paragraph needs to be enumerated.

  • { jump to the beginning of current paragraph
  • j skip blank line, move one line down
  • <C-v> emulates Ctrl-v, turns on Visual mode
  • } jump to the end of current paragraph
  • k skip blank line, move one line up

required region selected, we can make multi row edit:

  • I go into Insert mode and place cursor in the beginning of each line
  • 0. is added in the beginning of each line
  • <Esc> to change mode back to Normal

You should get list prepended with zeros. If you already have such, you can omit this part.

  • vip select inner paragraph (list prepended with "0. ")
  • g<C-a> does the magic

I have found it easier to enumerate with zeroes instead of omitting first line of the list to enumerate as said in documentation.

Note: personally I have no mappings. It is easier to remember what g <C-a> does and use it directly. Answer above describes usage of pure <C-a> which requires you to manually count whatever, on the other hand g <C-a> can increment numbers with given value (aka step) and have it's "internal counter".

outoftime
  • 715
  • 7
  • 21
  • You put "`g` does the magic" when I think you meant ``. Also, this is awesome. Could you help me understand the difference between `g ` and ``? The latter just increments numbers. Also, is there some pattern to using `g`? I know to join lines I use shift+j but to join without spaces I use `g` then shift+j. Is it some sort of global shortcut modifier? – user1717828 Feb 15 '21 at 03:15
  • type `:help CTRL-A` in vim you will see help for `{Visual}g CTRL-A`. I doubt that `g` modifier has common meaning for each command. I suppose `g` is used as A in alphabet - first and most handy character. Also it is just IMHO, I haven't read all user manual\reference docs yet. – outoftime Feb 15 '21 at 17:21
1

Create a map for @DmitrySandalov solution:

vnoremap <silent> <Leader>n :<C-U>let i=1 \| '<,'>g/^/s//\=i.'. '/ \| let i=i+1 \| nohl<CR>
Konstantin Glukhov
  • 1,898
  • 3
  • 18
  • 25