Visual Mode
Most of the time, operators and motions (dw, 3dd, y$) get you where
you need to go without ever selecting anything by hand. But sometimes you
want to see exactly what you’re about to act on before you commit to it.
That’s what Visual mode is for.
Three flavors of selection
v starts character-wise Visual mode. Move around with any motion you
already know (w, $, j, whatever), and the selection grows from where
you started to wherever your cursor is now.
V (capital) starts line-wise Visual mode. Instead of selecting
characters, it selects whole lines, which is what you want most of the
time when working with lines of code or text rather than fragments of
them.
Ctrl-v starts block Visual mode, also called Visual Block. This selects
a rectangular block of text, useful for things like adding the same
prefix to the start of several lines at once. It’s less common for
prose, more common when editing code or tables.
Once you’ve selected something
Selection alone doesn’t do anything. It’s the setup for a command. With text selected in any Visual mode:
ddeletes the selectionyyanks (copies) the selectioncdeletes the selection and drops you into Insert mode to type a replacement>and<indent or unindent the selected lines
The pattern should feel familiar: select first, then run a command,
similar to how dw combines an operator with a motion, except here you’re
choosing the target visually instead of naming a motion.
Escaping Visual mode
Esc exits Visual mode without doing anything, exactly like it exits
Insert mode. If you start a selection and change your mind, Esc gets you
back to Normal mode with nothing altered.
Try it
Open a scratch file with a few lines of text and practice:
- Press
v, move a few characters to the right, then pressdto delete the selection. - Press
V, move down two lines to select three lines total, then pressdto delete them all at once. - Press
Vagain, select a couple of lines, and pressyto yank them, then move elsewhere andpto paste. - Type several lines that start with a shared word or character, then
use
Ctrl-v, move down to cover a few lines, and try deleting the first column withd. - Start a
vselection, then pressEscbefore running any command. Confirm nothing changed.