2021-03-23
Discovering Vim's Visual (selection) mode
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.)