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 |
|---|---|
|
Toggle current line comment |
|
Block current line comment toggle |
Operation in visual mode:
key |
Operation details |
|---|---|
|
Selection row comment toggle |
|
Selected block comment toggle |
How to remember key bindings#
gc→ go comment (comment operation)gb→ go block (block comment)gcc→ go comment current on current lineIt is consistent with Vim’s
gprefix culture and is designed to be easy to remember.
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
Check comment string#
The comment syntax for the current file type can be found below:
Based on this value, comment.nvim selects the appropriate comment symbol.