How do I comment out multiple lines at once in Vim without a plugin?
Answer
<C-v>I#<Esc>
Explanation
Vim's Visual Block mode lets you prepend characters (like comment markers) to multiple lines simultaneously. Select a vertical column, enter Insert mode for the block with I, type the comment character, then press <Esc> — Vim replicates the insertion across every selected line. This works in stock Vim/Neovim without any plugin.
How it works
<C-v>— enter Visual Block modej/k(or arrow keys) — extend the selection downward/upward to cover all lines to commentI— enter Block Insert mode (inserts at the start of the block on every line)- Type the comment character, e.g.,
#for shell/Python,//for C/JS,--for Lua <Esc>— apply the insertion to all selected lines
Example
Before:
export FOO=bar
export BAZ=qux
export QUX=quux
Select all 3 lines with <C-v>2j, press I, type # , press <Esc>:
# export FOO=bar
# export BAZ=qux
# export QUX=quux
Tips
- To uncomment: select the
#column with<C-v>, extend down, then pressdto delete the block - For
//comments, type both characters afterIbefore pressing<Esc> - Works with any column position — move right first to prepend at an indented position
- If lines have different lengths, use
$to anchor to end, but for prependingIis ideal