Comment.nvim provides intelligent code commenting that understands different languages and contexts, including embedded languages.
Configuration
Comment is configured in lua/user/comment.lua with Treesitter integration for context-aware commenting.
The plugin integrates with ts_context_commentstring to use the correct comment syntax based on the cursor position:
pre_hook = function(ctx)
-- Uses treesitter to determine the correct comment syntax
-- Handles embedded languages (e.g., JS in HTML, Lua in Vim)
end
Default Keybindings
Comment.nvim uses intuitive keybindings that follow Vim conventions.
Normal Mode
| Key | Action | Description |
|---|
gcc | Toggle line | Comment/uncomment current line |
gbc | Toggle block | Block comment current line |
gc{motion} | Line comment | Comment using motion (e.g., gcap for paragraph) |
gb{motion} | Block comment | Block comment using motion |
Visual Mode
| Key | Action | Description |
|---|
gc | Toggle line | Comment/uncomment selection |
gb | Toggle block | Block comment selection |
Usage Examples
" Toggle comment on current line
gcc
" The line is now commented/uncommented
Use Vim motions to comment multiple lines:
gc2j " Comment current line and 2 lines down
gc4k " Comment current line and 4 lines up
gcap " Comment entire paragraph
gcG " Comment from cursor to end of file
ggcG " Comment entire file
For languages that support block comments:
gbc " Toggle block comment on current line
gb2j " Block comment current line and 2 lines down
- Select lines in visual mode:
V
- Comment the selection:
gc
Vjjjgc " Select 4 lines and comment them
" Comment a function
gcif " Comment inside function (requires treesitter text objects)
" Comment inside curly braces
gci{ " Comment inside {}
" Comment around curly braces
gca{ " Comment including the {}
Language Support
Comment.nvim automatically detects and uses the correct comment syntax for each language:
| Language | Line Comment | Block Comment |
|---|
| Lua | -- | --[[ ]] |
| Python | # | - |
| JavaScript | // | /* */ |
| C/C++ | // | /* */ |
| HTML | - | <!-- --> |
| CSS | - | /* */ |
| Bash | # | - |
Context-Aware Example
In a Vue or React file with mixed HTML/JS:
<template>
<div>Hello</div> <!-- Pressing gcc uses HTML comments -->
</template>
<script>
const x = 1; // Pressing gcc uses JavaScript comments
</script>
<style>
.class { } /* Pressing gcc uses CSS comments */
</style>
Tips and Tricks
Use counts with comment commands:
5gcc " Comment 5 lines starting from current line
In command mode:
:10,20Commentary " Comment lines 10-20
:.,.+10Commentary " Comment current line and next 10
Dotting (Repeat)
Comment actions can be repeated with the . command:
gcc " Comment current line
j. " Move down and repeat (comment next line)
j. " Repeat again
The same key toggles comments on/off:
gcc " Comment line
gcc " Uncomment line
Integration
Treesitter Integration
Comment.nvim uses Treesitter to determine the correct comment type based on:
- Current cursor position
- Surrounding code context
- Language at point (for multi-language files)
This ensures you always get the right comment syntax, even in complex files.
Text Objects
When used with Treesitter text objects, you can comment semantic code blocks:
gcif " Comment inside function
gcic " Comment inside class
gcaa " Comment around argument
Configuration Customization
While the default configuration works well, you can customize comment strings if needed:
require('Comment').setup({
toggler = {
line = 'gcc', -- Line-comment toggle
block = 'gbc', -- Block-comment toggle
},
opleader = {
line = 'gc', -- Line-comment operator
block = 'gb', -- Block-comment operator
},
})
Use gcc in normal mode to quickly toggle comments. It’s one of the most frequently used commands in coding.
The context-aware commenting means you don’t need to remember different comment syntaxes for different languages or file sections.
Common Use Cases
Debugging
Quickly comment out code blocks when debugging:
gcip " Comment entire paragraph/block
Documentation
Comment explanatory notes:
gcc " Add comment on current line
A " Then type your note
Temporary Removal
Comment code you might need later:
V5jgc " Select and comment 5 lines