vim

How to increase/decrease all integers in the same line in Vim?


In Vim, ctrl+a is to increase an integer and ctrl+x is to decrease an integer.

For example, to modify the following code to append 1, 2, 3 to the array, I can simply put ctrl+a once in line3 and twice in line4:

array = []      # line1
array.append(1) # line2
array.append(1) # line3
array.append(1) # line4

Then it will becomes:

array = []      # line1
array.append(1) # line2
array.append(2) # line3
array.append(3) # line4

But it's not convenient while I want to increase more than one integer in a line. For example, I want to change:

rank1 = 1
rank1 = 1
rank1 = 1

to:

rank1 = 1
rank2 = 2
rank3 = 3

My question is, is it a convenient way to increase all the integer in the same line via one keystroke?


Solution

  • You first type the following two lines:

    array = []
    rank1 = 1
    

    Then put you cursor in line 2. Then type the following by order:

    qa
    yy
    p
    shift+v
    :
    s/\d\+/\=submatch(0)+1/g
    q
    5@a
    

    And here '5' can change to how many repeat you want.

    Ok, this works, but it becomes more complicated.