Comment out selection range in Neovim: Introduce comment.nvim with vim-plug#

When using Neovim, there are many situations where you especially want to comment out a certain range of code. In this article, we will introduce the lightweight Lua plugin comment.nvim using vim-plug to smoothly turn comments on and off according to file type.

Installation steps (vim-plug + init.vim)#

Add the following to ~/.config/nvim/init.vim:

" comment.nvim を追加
Plug 'numToStr/Comment.nvim'

" Luaプラグインの初期化
lua << EOF
require('Comment').setup()
EOF

After starting Neovim, run the following command:

:PlugInstall

The plugin is now installed and activated.

Basic operation method#

comment.nvim automatically uses appropriate comment symbols (e.g. //, #, , <!– –>, etc.) for each file type.

Operation in normal mode:

key

Operation details

gcc

Toggle current line comment

gbc

Block current line comment toggle

Operation in visual mode:

key

Operation details

gc

Selection row comment toggle

gb

Selected block comment toggle

How to remember key bindings#

  • gcgo comment (comment operation)

  • gbgo block (block comment)

  • gccgo comment current on current line

  • It is consistent with Vim’s g prefix culture and is designed to be easy to remember.

Check comment string#

The comment syntax for the current file type can be found below:

:set commentstring?

Based on this value, comment.nvim selects the appropriate comment symbol.

summary#

  • comment.nvim is the best option for comfortable comment operations in Neovim.

  • Easy to install with vim-plug

  • It automatically handles comments according to the file type, so it is convenient and does not require any settings.

  • Easy to remember with key bindings that follow Vim culture

Please try introducing it to improve development efficiency using Vim/Neovim.

Article information

author:

Mr. Takagi

Post date:

2025-05-29.. rubric:: Article information