vimtricks.wiki Concise Vim tricks, one at a time.

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

  1. <C-v> — enter Visual Block mode
  2. j / k (or arrow keys) — extend the selection downward/upward to cover all lines to comment
  3. I — enter Block Insert mode (inserts at the start of the block on every line)
  4. Type the comment character, e.g., # for shell/Python, // for C/JS, -- for Lua
  5. <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 press d to delete the block
  • For // comments, type both characters after I before 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 prepending I is ideal

Next

How do I visually select a double-quoted string including the quotes themselves?