tags:

views:

786

answers:

3

I like vim's visual mode. v for highlight/select chars or lines, Ctrl-v 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 exmaple, 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.

+3  A: 

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?

ryan_s
I like your way. It is what I want to do.
David.Chu.ca
+6  A: 

Not exactly a programming question, but what the heck. 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)

reedstrm
Nice, I didn't know vim would let you operate on a selection like that.
ryan_s
A: 

Thanks for all the answers so quickly. That almost resolves my problem. However, I just realized that actually, there some cases when the chars I want to delete or replaced are not always at the end of selected lines. This makes the case more complicated. Basically, if there were a way to select or highlight irregular area, then I could visual mode editing commands to do delete or modifications. Not sure if you can select some areas by visual mode?

In a list control, you can select some lines by hold shift or control keys to make selection. How about visual mode?

David.Chu.ca
You should consider editing your original question and/or posting a new one(s).
graywh