level 06
Visual Mode
Select by character, line, and block, then act on the selection.
Visual mode is Vim with training wheels you never have to take off. Select first, see exactly what you’ve got highlighted, then hit an operator. For most edits a text object is faster (no selecting, just ci"), but some jobs are far clearer when you can see the range. And one of them, editing in columns with Visual block, has no real equivalent anywhere else.
Entering Visual Mode
v → character-wise
V → line-wise
Ctrl+v → block-wise (column selection)
gv → re-select the last selection
Operators on a Selection
d c y → delete / change / yank
> < = → indent / de-indent / auto-indent
~ u U → toggle / lower / upper case
Visual Block (Ctrl+v)
The unique one: select a rectangular region. To add text to the start of many lines — Ctrl+v, select down with j, press I, type your text, press Esc, and it applies to every line. Invaluable for prefixes, columns, and aligned data.
Character-wise selection
- On
quick, pressv, theneto select the word; presseagain to extend. - Press
Escto cancel. - On
qofquick, pressveto select, thendto delete; undo withu. - Select
brown foxwithv+ motion and press~to toggle case.
goal v plus a motion selects a region; operators act on it.
Line-wise selection
- On the
forline, pressV, thenjthree times to select the block. - Press
>to indent it one level. - Press
gvto reselect, then<to un-indent. - Select the two
const-style lines and pressd; undo.
goal V selects whole lines; > and < indent the block.
Visual block — add a prefix
- Press
Ctrl+vto enter Visual block. - Press
Gto extend the cursor down every line. - Press
I, type-(dash space). - Press
Esc— every line now starts with-.
goal Apply the same text to the start of many lines at once.
Visual block — delete a column
- Put the cursor on the
|at the start of the first line. - Press
Ctrl+v, thenjthree times to extend down. - Press
luntil the first column is fully selected. - Press
dto delete the column.
goal Block mode removes a rectangular region.
Visual block — append to line ends
- Press
Ctrl+v, thenGto select down. - Press
$to extend the selection to each line’s end. - Press
A, type;, pressEsc— every line now ends with;.
goal $ extends the block to each line's end before A.
Capstone — turn raw rows into a typed enum
Convert this flat list into a block of object entries — RED: "red", and so on — using Visual mode for every step instead of editing line by line:
- Block prefix.
Ctrl+v,G, thenIand two spaces,Esc— indent all five. - Block suffix, part one.
Ctrl+v,G,$, thenA, type: "",Esc— every line now ends with: "". - Fill the value with a lowercased copy of the key. On line 1:
0wto reach the key,yiwto yank it. Pressf"to land on the opening quote,a, thenCtrl+r 0to paste the key between the quotes,Esc. Lowercase it in place withviwu. That precise little routine is a perfect macro — record it withqa … qand replay4@adown the rest. - Trailing commas.
Ctrl+v,G,$,A, type,,Esc. - Reselect any line range with
Vand press=to tidy the indentation.
goal Chain character-wise, line-wise, and block edits into one transform.