Home VIM Vim magic – Visual mode

Vim magic – Visual mode

by admin

A little bit about highlighting mode for those whoare toolazy to go through vimtutor, or those who still doubt that Vim is really handy.
Selection mode in Vim is one of those grandiose things (along with * , = and text-objects ), which make this editor especially handy. As you already know, Vim has a Visual mode that allows you to select text and then do things with this selection, such as register it, format it, replace it with a regular expression, or whatever. To get into this mode, you press v and then move the cursor with normal commands that move the cursor in normal mode, thus expanding the selection area.
There are several selection modes: normal, line and block. The normal mode is activated by the command v and selects text from character to character. Line-by-line is turned on with the command V ( Shift+v ) and selects lines in their entirety. Blocky is turned on by the command Ctrl+V (under Windows it can be turned on Ctrl+Q if Ctrl+V is taken under clipboard text insertion) selects a vertical block of text (does not grab the beginning and ends of lines in the middle of the selection, as it does in normal mode).
Now, a little bit of special, Vim magic. Let’s say you start highlighting text, but you notice that the beginning of the highlighting is not where you wanted it to be. In most editors, you would need to reset the selection and re-select the text. In Vim, you can press o and expand the selection in the other direction. If, however, you are in block mode o will move the cursor diagonally across the block, i.e. from the top left corner of the block to the bottom right corner, for example. If you want to change the selection using the remaining two corners, press O ( Shift+o ).
But that’s not all. Imagine that you started highlighting in normal mode, but then decided that you should have done it in line mode. Just press V and the selection mode will change to line mode and you won’t lose the current selection. You can change modes as you want without resetting the selection, and I think that’s really handy. And lastly, if you need to select the same bit of text that you had selected the last time (for example you accidentally deselected it, or you did something with it and want to do something else) – just press gv

You may also like