Block Comment with Ctrl-V + I

Add a comment marker to N consecutive lines.

Keys: Ctrl-V, I, Esc

Ctrl-V to enter block visual, j or count+j to select rows, I to insert at left edge, type the comment marker, Esc โ€” the marker appears on every line.

Block Comment

Vim has no built-in "comment this block" command โ€” but block-Visual + capital-I gives you a one-trick replacement that works in any language.

Comment 5 lines with //
KeyNote
Ctrl-VEnter block Visual
4jExtend down 4 more lines (5 total)
IInsert at left edge of block
// Type the marker
EscMarker appears on all 5 lines

Uncomment

Strip 3 chars ("// ") from N lines
KeyNote
Ctrl-VBlock Visual
4jDown 4 lines
lllRight 3 columns to cover "// "
dDelete the rectangle

Worked example โ€” Ctrl-V jjj I//<Esc>

Block-prefix many lines at once.

Step 1 ยท
pattern.block-comment
Step 2 ยท Ctrl-V jj ยท Ctrl-V jj โ€” three rows selected.
Ctrl-V jj โ€” three rows selected.
Step 3 ยท I// ยท I// โ€” typing on the first row only.
I// โ€” typing on the first row only.
Step 4 ยท Esc ยท Esc โ€” Vim replays the insertion on every selected row.
Esc โ€” Vim replays the insertion on every selected row.

The trick: in block-Insert mode, the typing only shows on row one until you press Esc.

โ–ถ Try this in the simulator

Watch

See also: Block Insert and Append, The Three Visual Modes