Search code examples
vimrecordauto-increment

Increment the number under the curser in vim editor by a pattern


Is it possible to increment the number under the cursor in vim editor by a pattern. For example I need it like this:

Before Edit:

15 Apple
15 Orange
15 Mango
15 Grape
15 Pineapple
15 Tomato
15 Plum
15 Cherry

After Edit

18 Apple
19 Orange
20 Mango
21 Grape
22 Pineapple
23 Tomato
24 Plum
25 Cherry

Means in first line its incremented by 3, 2nd line by 4, 3rd line by 5 and so on. I am thinking of recording and play back. But each line the incremented value is incremented by one. (pattern like 3, 4, 5, 6, 7, 8, etc,.)

Please let me know any one have the solution.

If we can achieve like below is also appreciated.

16 Apple
17 Orange
18 Mango
19 Grape
20 Pineapple
21 Tomato
22 Plum
23 Cherry

Pattern starts with 1. (patterns is 1, 2, 3, 4, 5, 6, etc,.)


Solution

  • except for variable to control the +1, there is another way to do it. First take a look the line number of the first line in your text block, say it was 7, then you can:

    :7,$s/^\d\+\ze/\=submatch(0)+3+line('.')-7/
    

    For example, if it is the first line, you can change the 7 into 1:

     :%s/^\d\+\ze/\=submatch(0)+3+line('.')-1/