Visual mode and block editing

Vim · Practical · 10 min

Visual mode is how you see exactly what an operation will affect, and blockwise mode edits columns.

Visual mode lets you select text first and then act on it. There are three kinds:

  • v characterwise, V linewise, Ctrl-v blockwise (a rectangle).

Extend the selection with any motion or text object (vip selects a paragraph, vi" a string), press o to jump to the other end of the selection, and gv to reselect the last selection. Then apply an operator: d, y, c, >, <, ~ (toggle case), u/U (lower/upper), J (join), or : to run an Ex command on the selected lines (Vim fills in :'<,'>).

Block mode is the multi-cursor of Vim. Select a column with Ctrl-v and j, then:

  • I text Esc inserts the text at the start of the block on every line.
  • A text Esc appends after the block; with $ first, it appends at the end of each line even if lines differ in length.
  • c replaces the block on every line, r replaces every selected character.

The text appears on all lines only after you press Esc.

Visual mode is great while learning and for irregular selections, but for repeatable work prefer operator + text object (dap rather than vapd): Visual selections repeat with . by size, not by structure.

Example

Vjjd        " select 3 lines linewise and delete them
vi"y        " yank a string's contents
gv          " reselect the previous selection
Vip>        " indent the whole paragraph
" comment out 4 lines with a block insert:
Ctrl-v 3j   " select a column over 4 lines
I# <Esc>    " '# ' inserted on every line
" append ';' to lines of different lengths:
Ctrl-v 3j $ A;<Esc>

Remember

  • v characters, V lines, Ctrl-v rectangular blocks
  • Block I/A insert on every line, shown after Esc
  • gv reselects; o jumps to the other end of the selection

Watch out for

  • Expecting the block insert to appear while typing; it is applied to the other lines only when you press Esc.

Check yourself

  1. You selected 5 lines with Ctrl-v and typed `I// ` then Esc. What happens?

Sign in to answer, see the explanations, and get this lesson back as a quick review later.

Official docs: vimhelp.org/visual.txt.html