Unlock your productivity with Vim For Developers book!

How To Comment Code In Vim

November 12, 2021

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.

Vim For Developers

Vim For Developers

Learn Vim and upgrade your productivity to the next level by building the IDE of your dreams.