Vim From Scratch

How To Comment Code In Vim

In Vim there is no out-of-the-box way to comment code. But some plugins will make that happen for us.

I suggest using the battle-tested tpope/vim-commentary by Tim Pope.

It introduces a special command (operator) gc, which fits quite nicely into Vim world.

  • gcc comment/uncomment the current line
  • gc comment/uncomment the selection in Visual mode
  • gcap comment the entire paragraph (it supports the text objects (:help text-objects))

More options

Among other alternatives, there are nerdcommenter and tcomment, which you might want to also check out.