1

I have this code,

id1 = item.check([[-10, -10, a1, b1], [10, 10, a1, b1]])
id1 = item.check([[-10, -10, a1, b1], [10, 10, a1, b1]])
id1 = item.check([[-10, -10, a1, b1], [10, 10, a1, b1]])
id1 = item.check([[-10, -10, a1, b1], [10, 10, a1, b1]])

I want to replace integer that following the string and have this output

id1 = item.check([[-10, -10, a1, b1], [10, 10, a1, b1]])
id2 = item.check([[-10, -10, a2, b2], [10, 10, a2, b2]])
id3 = item.check([[-10, -10, a3, b3], [10, 10, a3, b3]])
id4 = item.check([[-10, -10, a4, b4], [10, 10, a4, b4]])

I go each line and conduct each command, for example in second line :s/\s\d+/2/g and so on. But nothing happens.
This is how I came up,
:s is replacing, \s is character, \d+ is int, /2 is output, /g is global for the line.
How can I improve? Thanks in advance!

1
  • 1
    Ps \s is a space character is patterns. :help pattern
    – D. Ben Knoble
    Commented Dec 23, 2020 at 13:09

1 Answer 1

3

Using regex/substitution won't help much. Fortunately, Vim has a command for generating a sequence down one column.

Use ctrl-v to select the third column (the digits) on all but the first row and type g then ctrl-a.

That will add 1 to the selected number on the second row, 2 to the number on the third row, and so on.

Note that you can't apply this to multiple columns of numbers at once. You'll need to run it separately five times, in your case. However, since the numbers are the same in each of the columns you should be able to use the repeat command after you run it once: move the cursor to the appropriate column of the second row (no need to visually select the whole column) and hit ..

So, once you've placed the cursor at row 2 column 3 you could do, for example...

<c-v>{R-2}jg<c-a>fal.f1.fal.f1.

Where {R-2} is replaced with total rows - 2.

See :h v_g_CTRL-A.

2
  • it works okay to third column, however, when I try to others (a2,b2,...), it adds number to next line's third column.
    – jayko03
    Commented Dec 23, 2020 at 5:31
  • 1
    You can only select numbers for this to work. That means you need to run it once for each column of numbers. You can use ., though. I'll note this in my answer.
    – B Layer
    Commented Dec 23, 2020 at 5:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.