4

Each row is:

Or(a=a[0], b=b[0], out=out[0]);
Or(a=a[0], b=b[0], out=out[0]);
Or(a=a[0], b=b[0], out=out[0]);

and I need:

Or(a=a[0], b=b[0], out=out[0]);
Or(a=a[1], b=b[1], out=out[1]);
Or(a=a[2], b=b[2], out=out[2]);

I tried this solution as:

:let t=[]  # then ctrl-v
:s/\d/\=len(add(t,1))/g

But this increments each subsequent number, not per row. I couldn't find/understand other solutions.

Thank you!

2 Answers 2

3

Do you mind some Awk?

:%!awk 'gsub(/[0-9]+/,i+0){i++}1'

This means:

  • gsub(/[0-9]+/,i+0) If there is any number in the line, substitute it by i+0.
  • {i++} If a substitution occurred in this line, increment i.
  • 1 is a short for "print the resulting line".

In case you are wondering, i+0 forces the conversion of i to integer in the case i is unset (case which happens in the first line).

4

If you visual highlight with ctrl-v a column of zeros and press g ctrl-a it will consecutively increase them. If you want to leave the first one as a zero, you should omit that from the selection. Also, if there are many columns, like in your example, it's a good idea to start from the rightmost, since if the numbers increase enough to require more digits anything to the right of the original column will now be misaligned after the digit increase.

If you have a lot of columns awk will probably be better, but for a few, I this is probably easier to do (and remember, at least for me :)).

4
  • 1
    Awesome! Just when you think you've learned all you need to know, the Vim help surprises you with a new gem: :help v_g_CTRL-A. Thanks, @fbence!
    – Phil R
    Jul 20, 2022 at 17:43
  • 2
    Just a note: v_g_^a is dotable (. works), as any command. Just put your cursor on the second column, no need to enter visual mode again (that wouldn't work).
    – Biggybi
    Jul 20, 2022 at 19:44
  • @Biggybi nice one! thanks!
    – fbence
    Jul 20, 2022 at 21:12
  • @PhilR exactly how I usually feel :D
    – fbence
    Jul 20, 2022 at 21:13

Your Answer

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

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