David.Chu.ca
David.Chu.ca

Reputation: 38644

VIM visual mode: highlight last 2 chars in each line?

I like Vim's visual mode. v for highlight/select chars or lines, Ctrlv for rectangle highlighting, as far as I know (I am a beginner). Is there any way to use visual mode to highlight last two chars, for example, on each line for some selected lines? The selected lines are in different length. Basically, I would like to find a quick way to remove the last two chars for some selected lines. Not sure I can use visual mode to highlight irregular area.

Upvotes: 3

Views: 3022

Answers (2)

reedstrm
reedstrm

Reputation: 382

My approach to this sort of problem is to use line selection (shift-V, cursor movement) to select the lines-of-interest, then type:

 :s/..$//

That's a substitution, using the regex ..$ which will match the last two characters at the end of the line. Then substitute 'nothing' i.e. delete.

In vim, once you hit the : with a line selection active, the command prompt will actually show:

:'<,'>

Which is the start and end of selection addresses for the next command (s in this case)

Upvotes: 9

ryan_s
ryan_s

Reputation: 7954

I can't think of a way to do this in visual mode, but you could use a command like this to do it...

:10,20 normal $xx

This would go to each line between line number 10 and 20, use $ to go to the end of the line, and then use x twice to delete two characters. Normal just tells vim to use the following symbols as if they were keyboard shortcuts entered in normal mode (i.e after hitting esc).

Does that help?

Upvotes: 3

Related Questions