0

I'm needing to add 184 to multiple (but not all) columns in Vim editor. The following command only adds 184 to the very first column, even if the second or the third column is the only highlighted column in visual block mode, only the first column gets added onto. I can't use /g at the end cause there's numbers that I can't add to in each line as well

:s/\d\+/\=submatch(0) + 184
1
  • Welcome to Vim :-) Do you still have something open in your question? Otherwise may be could you accept one of the solutions using the v button next to the arrow voting buttons. It allow the question to rest :-) Apr 24, 2023 at 6:13

2 Answers 2

1

<c-a> can be used in Visual Block mode e.g. <c-v>.

Lets say you have the following text:

1 2 3
1 2 3

You can select the 2nd column with f2<c-v>j then add your number via 184<c-a>

Then you will have:

1 186 3
1 186 3

There are other methods as well. However, I tend to favor shelling out to use awk. The following command will add 184 to the 2nd column:

awk '$2+=184' input.txt

You can do this from Vim with a range and the filter command, !. Select you lines visually then run :!awk '$2+=184'

For more help see:

:h v_CTRL-A
:h :!
:h :range
0

If you prefer to stay in Vim you can do:

:'<,'>s/\%V\d\+/\=submatch(0)+184/g

Vim is very much line oriented. The range '<,'> specify that the substitution should only occur in the line selected.

But the \%V only match (with zero width) if the next character is within the selection.

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.