C.W.K.
Stream
Lesson 03 of 06 · published

Visual Mode — Select Then Operate

~11 min · vim, visual-mode, selection

Level 0Trapped
0 XP0/35 lessons0/12 achievements
0/100 XP to next level100 XP to go0% complete

The safety net for complex selections

Most of the time, operator + text object is faster than Visual mode. But for ad-hoc selections — "this and the next four lines plus that bit at the end" — Visual mode lets you see exactly what you're about to operate on, then press the operator last.

Three flavors of Visual

  • v — character-wise. Anywhere from one char to multiple lines.
  • V — line-wise. Whole lines only, regardless of where on each line you are.
  • Ctrl-v — block (column). The killer feature for columnar editing.

Select first, then verb

Once you're in Visual mode, every motion expands or shrinks the selection. When the selection is what you want, press an operator: d deletes it, y yanks it, c changes it, > indents it, = auto-formats it, : drops you into a Command-line scoped to the selection ('<,'>).

Block selection — columnar editing

This is the move that other editors can barely match. Press Ctrl-v, move down to define a column, press I to insert at the start of the block, type your text, press Esc — and Vim applies your edit to every line in the block. Same for A (append after column), c (change column), d (delete column).

Block-Insert quirk: after typing in a Ctrl-v + I session, your text appears only on the first line until you press Esc. Then Vim duplicates it to every selected line. It's normal; don't panic and abort.

Reselect the last selection

gv brings back the last visual selection — invaluable when you operate on a region, realize you wanted the next operator, and want to redo the selection without re-defining it.

The other-end pivot

Inside Visual mode, o jumps to the other end of the selection — useful for extending the selection backward when you started forward, or vice versa.

Code

Three Visual flavors·vim
v       " character-wise visual
V       " line-wise visual
Ctrl-v  " block visual (column)
gv      " reselect last visual
o       " inside Visual: pivot to other end of selection
Operate on a Visual selection·vim
viw d            " select inner word, delete it
Vjjj d           " line-wise: select 4 lines, delete
v$ y             " select to end of line, yank
Vi( >            " select linewise inside parens, indent
vap =            " select a paragraph, auto-format it
:'<,'>sort       " sort the lines you selected
Block editing — add a prefix to many lines·vim
" Comment-out 5 lines of Python (starting from current cursor):
Ctrl-v       " enter block mode
4j           " extend down 4 more lines (5 total selected)
I            " insert at the start of the block
# <Esc>      " type '# ' then Esc — Vim mirrors it to every line

" Delete a column — say, 4 leading spaces:
Ctrl-v 3l 4j d   " select 4 cols × 5 lines, delete the block

External links

Exercise

Open a Python or JavaScript file. Use Ctrl-v + I to add a // or # comment prefix to a block of 5 lines. Then use Ctrl-v + d to delete a column of 4 leading spaces from a different block. Pay attention to the moment when the prefix appears on all lines after Esc — that's the unique Vim move.

Progress

Progress is local-only — sign in to sync across devices.
Spotted a bug or have feedback on this page?Report an Issue

Comments 0

🔔 Reply notifications (sign in)
Sign inPlease sign in to comment.

No comments yet — be the first.