Block Comment with Ctrl-V + I

Add a comment marker to {n} consecutive lines.

Keys: Ctrl-V, I, Esc

Ctrl-V to enter visual-block mode, 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 visual-block mode + capital-I gives you a one-trick replacement that works in any language.

Comment 5 lines with //
KeyNote
{key:Ctrl-V}Enter visual-block mode
{key:4}{key:j}Extend down 4 more lines (5 total)
{key:I}Insert at left edge of block
`//`Type the marker
{key:Esc}Marker appears on all 5 lines

Uncomment

Strip 3 chars (// ) from N lines
KeyNote
{key:Ctrl-V}visual-block mode
{key:4}{key:j}Down 4 lines
{key:l}{key:l}{key:l}Right 3 columns to cover //
{key:d}Delete the rectangle

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

Block-prefix many lines at once.

Step 1 ยท
pattern.block-comment
Step 2 ยท Ctrl-Vjj ยท 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.

Watch

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