0

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,.)

imbichie
  • 1,510
  • 3
  • 13
  • 23
  • Have you tried anything so far? By showing your attempts and describing what parts you are having difficulty with, we get a better understanding of your proficiency and can give a more precise answer. As it stands, it just looks like you've posted a requirement and want someone to write your code for you. – Ingo Karkat Mar 19 '15 at 12:07
  • You'll have to customize VIM a little with a script or macro, but a good start is to look at `:help ctrl-a`. – Codie CodeMonkey Mar 19 '15 at 12:28
  • Hi all, Please dont misunderstand me. I know the how to increment (ctrl-a) or decrement (ctrl-x) the number under the cursor with a specific value. But here the case is different. I have thousands of lines in my file. and each line I need to increment the number under the cursor with a value which is one increment than the previous line. Here duplication wont work because each line is entirely different. the code I posted is an example which just describe what I need. Actual case is different. – imbichie Mar 20 '15 at 03:23

2 Answers2

1

use a var save count, and combine g and s

:let n=15+3|g/^\d\+/exec "s//".n|let n=n+1

another way, use (ctrl-a) first move to 15, change to 17. then "ayiw save to register a. then you can record macro, use:

qqciw<C-r>a<esc><C-a>"ayiwjq, last repeat macro q @q

SolaWing
  • 1,652
  • 12
  • 14
1

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/
Kent
  • 189,393
  • 32
  • 233
  • 301