1

In visual selection mode, accessed with ctrl-v, I can replace the character I want (say Z to be replaced by A) within my selection by typing : followed by s/Z/A.

This is extremely useful.

Here is a case where it works:

12_0test
12_1test
12_2test
12_3test
12_4test
12_5test
...

I select the "2" in the "12" (ctrl-v and use arrow keys to select all rows with 2 which results in a column of 2 being highlighted) and do : followed by s/2/3. I get what I'm expecting:

13_0test
13_1test
13_2test
13_3test
13_4test
13_5test
...

But I just came across a case where, when in a series of lines the first two characters are the same (here 11), block selecting the 2nd digit and trying to replace it with another digit will replace the 1st digit.

Here is what I mean.

11_0test
11_1test
11_2test
11_3test
11_4test
11_5test
...

I select the column of the second "1" in the "11", then : followed by s/1/2; but I end up with this undesired output:

21_0test
21_1test
21_2test
21_3test
21_4test
21_5test
...

Why is it so?

1

Substitution is a line-wise operation so even though you've selected just one column the operation applies to the whole line. Try doing s/1/2/g. If this was a character-wise operation this would only replace the second column, too. But it replaces all '1's with '2'.

Update: I should point out that you can accomplish what you want by entering r2 after the column is selected...replace is a single character operation.

  • In OP's example :s/\%V1/3 would have also worked. – Mass Sep 30 '17 at 14:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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