I am trying to increment the numbers in a column of a file by a constant value using:
:s/\d\+/\=submatch(0)+8192
where 8192
is the constant I want to add.
My file has the below structure:
ATOM 32760 MW4 wate 8190 6.520 15.778 26.020 0.00 0.00 Mw4
ATOM 32761 OW1 wate 8191 6.550 20.128 25.950 0.00 0.00 O
ATOM 32762 HW2 wate 8191 6.660 21.078 25.990 0.00 0.00 H
ATOM 32763 HW3 wate 8191 7.440 19.778 25.870 0.00 0.00 H
ATOM 32764 MW4 wate 8191 6.680 20.208 25.950 0.00 0.00 Mw4
ATOM 32765 OW1 wate 8192 1.280 16.158 26.060 0.00 0.00 O
ATOM 32766 HW2 wate 8192 1.350 17.108 26.160 0.00 0.00 H
ATOM 32767 HW3 wate 8192 1.800 15.958 25.280 0.00 0.00 H
ATOM 32768 MW4 wate 8192 1.360 16.258 25.970 0.00 0.00 Mw4
ATOM 32769 OW1 wate 1 17.610 8.049 75.290 0.00 0.00 O
ATOM 32770 HW2 wate 1 17.860 7.549 76.060 0.00 0.00 H
ATOM 32771 HW3 wate 1 16.680 8.249 75.420 0.00 0.00 H
ATOM 32772 MW4 wate 1 17.520 8.009 75.410 0.00 0.00 Mw4
ATOM 32773 OW1 wate 2 16.200 7.639 85.230 0.00 0.00 O
ATOM 32774 HW2 wate 2 15.720 8.139 85.890 0.00 0.00 H
ATOM 32775 HW3 wate 2 17.060 8.049 85.200 0.00 0.00 H
ATOM 32776 MW4 wate 2 16.250 7.759 85.320 0.00 0.00 Mw4
ATOM 32777 OW1 wate 3 5.190 10.149 77.870 0.00 0.00 O
ATOM 32778 HW2 wate 3 6.070 10.009 77.530 0.00 0.00 H
ATOM 32779 HW3 wate 3 5.020 11.079 77.730 0.00 0.00 H
At present, my command selects the first integer column which is next to ATOM
column. I am visually selecting the values from 1
to end of the file (which is 5th column).
However, what I want to do is increment the values starting from 1
to the end of the column with 8192
, essentially continuing the numbering from 8192
. Could someone suggest what I need to do to achieve this?
Your original command was almost there, the only tiny detail missing is \%V
to constrain the :substitute
command to the Visual-block selected area.
Like most Ex commands, :substitute
works on whole lines by default, even in Visual-block mode.
Let's put a \%V
in your original command to make it work on the selected parts only:
:'<,'>s/\%V\d\+/\=submatch(0)+8192/
The respective documentation can be found in :help /\%V
.
There's also a sentence below :help pattern-delimiter
which nicely sums it up:
In Visual block mode, use /%V in the pattern to have the substitute work in the block only. Otherwise it works on whole lines anyway.
This will get you almost there but suffers from the same indentation problems as the macro-based answer. To keep the columns properly indented, you can match and discard the leading whitespace using a capture group. Just make sure the Visual-block selection is four characters wide:
:'<,'>s/\%V\s*\(\d\+\)/\=submatch(1)+8192/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With