Discovering Vim's Visual (selection) mode

March 23, 2021

Usually, when I want to operate on a block of text (for example to reflow it) I can select it with one of Vim's motion commands, such as '}' for a nominal paragraph. Vim's notion of a paragraph is sufficiently all-encompassing that this works for a lot of things that aren't strictly paragraphs but are, for example, ed style diffs. When I want to select something that doesn't match up with this, such as only part of a quoted email message, my traditional solution has been marks.

Vim's Visual mode is an alternative way of making these selections. To be a bit unkind to vim, it's vim's version of what would be mouse based selection in a GUI editor such as sam. Since it has visual feedback, it has many of the advantages of mouse based selection, but it also lets you take advantage of all of Vim's motion commands for moving around. In effect it's like a version of setting a mark that visually shows me what a "`a,." address would cover, and I can back up or move forward from the results of, say, } to jump the the end of the nominal paragraph.

(More likely I will start visual mode and then move line by line until I'm where I want to be. I often take a brute force approach to editing in Vim.)

As covered in the documentation, visual mode comes in three varieties; v for character by character selection, V for line by line selection, and Ctrl-v for a block selection (which I haven't used). Even with a character by character selection, some Vim commands (including many that I use) will operate on all lines with some selected characters in them. However, I probably want to get into the habit of properly using V instead when I'm planning on doing a line-wise operation, such as using ! to reflow quoted text with par.

I'm not sure how I stumbled into actually understanding visual mode. I'm sure I've accidentally hit v periodically in command mode and been irritated by the resulting mystery highlighting (fortunately hitting ESC a couple of times makes it go away), but recently I learned what it was actually for and motivated myself to start using it in some cases where I expect it to be more convenient than the alternatives.

(This entry is one of the ones I write partly to get a Vim feature to stick in my mind and partly to do some more reading on it so I know more of what I'm talking about.)

Comments on this page:

By James (trs80) at 2021-03-24 10:42:21:

I use Ctrl-V for inserting things in vertical columns (mostly prepending, but sometimes in the middle of lines). I never connected it with the idea of it just being visually marking out ranges, but that's clearly what it is. No idea how I stumbled across it.

By dozzie at 2021-03-24 11:43:09:

As James said, visual block allows to insert text at the beginning/in the middle/at the end of lines (yes, this too).

  • to add stuff at the beginning of selection, hit I
  • to add stuff at the end of selection (but before non-selection), hit A

These two are variants of normal mode commands, modified for what makes sense in visual block.

To add stuff at the end of bunch of lines, go into visual block, select lines, hit $ or End, then A. Vim with $ in visual block doesn't select past the EOL, so the right edge of the selection is "ragged".

Of course selection can be deleted; I often use this to uncomment blocks of code (and to comment it again, inserting from visual block). Pasting copied/yanked out blocks also works nicely.

Then there's virtualedit option, controlling when you can put cursor after the end of line. I like virtualedit=block, so I can only move past the EOL in virtual block mode.

Another handy command for all the visual modes is o, which swaps the beginning and end of the selection (the end is the cursor's position). You can extend or shrink the selection this way.

And the last one I use often, gv in normal mode restores last visual selection, so you can do stuff to it, select it again, and do something more.

By dozzie at 2021-03-24 11:46:55:

Ah, I forgot to mention, you can switch between visual, visual line, and visual block without losing your selection. Just hit appropriately, v, V, or CTRL+v in an already running visual mode.

By alfh at 2021-03-26 10:29:41:

Useful things off the top of my head:

  • In visual mode, you can use o to change which corner is moved; visual block mode also has O
  • gv reselects the previous visually selected block
  • You can press : in visual mode to run a command over the selection
Written on 23 March 2021.
« Portability has ongoing costs for code that's changing
HTTPS is really multiple protocols these days »

Page tools: View Source, View Normal, Add Comment.
Login: Password:
Atom Syndication: Recent Comments.

Last modified: Tue Mar 23 23:52:48 2021
This dinky wiki is brought to you by the Insane Hackers Guild, Python sub-branch.