Learn Vim / How to Comment Out Multiple Lines in Vim

How to Comment Out Multiple Lines in Vim

The answerUse visual block: Ctrl-v, extend down with j, press I, type // (or # ), press Esc — the comment appears on every selected line.

Try it on a real buffer

Comment out all four lines at once: Ctrl-v for block visual, 3j to reach the bottom, I to insert at the block's left edge, type “// ” (slash slash space), then Esc — and watch it appear on every line.

config = load()
validate(config)
apply(config)
print("done")

Canonical solution: Ctrl-v 3j I // Esc · par: 7 keystrokes (vimgolf rules — every keypress counts).

Why it works

Ctrl-v selects a rectangle. I inserts before it, A appends after it; on Esc the edit is replayed on every line the block touched. The closest thing vim has to multiple cursors.

Variations

KeysWhat it does
Ctrl-v 3j I // Escprepend // to four lines
Ctrl-v 3j I # Escsame for Python/shell
:2,5s/^/# /comment lines 2–5 with a substitute
Ctrl-v + xuncomment: block-select the markers, delete

Reading about keystrokes doesn't build keystrokes. Try this in a real vim buffer right now — the “The column of //” mission takes about a minute.

Practice free — no signup →

Keep going