Skip to content

Lesson 3 of 9 · 21m

Visual Mode and Text Objects Without Guesswork

Characterwise, linewise, and blockwise selection; operator-first editing; and the selection traps that produce mangled text.

Visual mode is useful, but it is not the center of Vim. Think of it as the fallback when an object is irregular or when you need to see exactly what will be affected. If you select everything visually all day, you are recreating mouse editing with extra steps.

The three visual modes mean three shapes

v        characterwise selection
V        linewise selection
Ctrl-v   blockwise selection

Characterwise follows the cursor across partial lines. Linewise always selects whole lines, including their newlines. Blockwise selects a rectangle. Most corruption bugs come from using the wrong shape: delete linewise text with a characterwise mental model and the newline behavior surprises you.

Press o while selecting to move the cursor to the other end of the selection. gv reselects the previous visual area — useful when you realize you indented the wrong side or want to apply a second operation.

Prefer operator-plus-object when it exists

vwhhhd      visual, fiddle around, delete — fragile
d3w         delete three words — precise
ci(         change inside parentheses — structural
dap         delete around paragraph — includes surrounding whitespace

Visual mode shows you what will happen, which is valuable for safety. Operator-plus-object is repeatable and composable, which is valuable for speed. The professional judgment is knowing when confirmation beats precision: irregular log output gets visual selection; structured code gets text objects.

Block visual is for columns, not magic

Ctrl-v earns its keep in tables, aligned assignments, and generated code:

Ctrl-v
jjj       extend the block down three lines
I-- <Esc> insert a comment prefix on every line
$A;<Esc>  append a semicolon at the end of every line in the block

The append trick requires $ before A: it extends the rectangle to the end of each line. Without it, A inserts at the block edge, not the actual line endings.

Visual commands worth knowing

u / U      lowercase / uppercase selection
> / <      indent / outdent selection
=          reindent selection
J          join selected lines
:'<,'>!cmd filter selection through cmd

After selecting, : prefills :'<,'>, the range for the visual selection. That is how visual mode and Ex commands cooperate: :'<,'>!sort -u pipes the selection through sort -u and replaces it with the output.

Field notes

  • gv is the answer to “I just had it selected.” Learn it before you need it.
  • A linewise delete removes the newline; a characterwise delete generally does not. Yanking behaves the same way.
  • If Ctrl-v is intercepted by your terminal or OS paste shortcut, use Ctrl-q where the terminal passes it through, or fix the terminal mapping.
  • Visual selection is not a prerequisite for editing. Ask “what object?” before pressing v.

Was this lesson helpful?

Sign in to rate