explaingit

numtostr/comment.nvim

4,645LuaAudience · developerComplexity · 2/5Setup · easy

TLDR

A Neovim plugin for toggling code comments with Vim-style keyboard shortcuts, gcc for line comments, gbc for block comments, with smart language detection for mixed-language files via treesitter.

Mindmap

mindmap
  root((comment.nvim))
    What it does
      Toggle line comments
      Toggle block comments
      Visual mode selection
    Key bindings
      gcc for lines
      gbc for blocks
      Motion combinations
    Smart detection
      Commentstring setting
      Treesitter integration
      Mixed language files
    Customization
      pre-hook and post-hook
      Setup function
      Plugin manager support
Click or tap to explore — scroll the page freely

Code map

Detail Auto

An interactive map of this repo's files and how they connect — its source is parsed live in your browser. Click Visualize to build it.

filefunction / class

Things people build with this

USE CASE 1

Toggle line or block comments on any code file in Neovim using gcc or gbc without leaving the editor.

USE CASE 2

Comment multiple lines or a visual selection at once using motion keys like gcip or gc2j.

USE CASE 3

Handle mixed-language files like HTML with embedded JavaScript by letting treesitter pick the right comment syntax automatically.

Tech stack

LuaNeovimTreesitter

Getting it running

Difficulty · easy Time to first run · 5min

Requires Neovim, treesitter support for mixed-language files needs the nvim-treesitter plugin installed separately.

In plain English

Comment.nvim is a plugin for the Neovim text editor that makes it easy to comment and uncomment lines or blocks of code using keyboard shortcuts, without leaving the editor. The plugin uses a keymap pattern familiar to Vim users. In normal mode, pressing gcc toggles a line comment on the current line, while gbc does the same with a block comment. A number prefix like 3gcc comments three lines at once. In visual mode, gc and gb comment whatever text is selected. Motion keys extend this further: gcip comments a whole paragraph, gc2j comments the next two lines below the cursor, and similar combinations let you target ranges of code using the same movement system as other Vim commands. Comment.nvim can detect the correct comment syntax for the file type you are editing using the commentstring setting. It also supports treesitter, a tool that parses code into a structured syntax tree. When a file mixes more than one language, such as a template file containing both HTML and JavaScript, treesitter support lets the plugin apply the right comment style for whichever language the cursor is currently inside. There are known limitations with JSX and TSX files. Two hook points called pre_hook and post_hook let users run custom Lua functions before or after the comment operation. These are useful for cases where the default behavior does not cover a particular comment style or integration need. The README includes an example showing how to wire in a companion plugin for better JSX comment handling. Configuration is done through a setup function call. Most options have sensible defaults and the full list is documented in the plugin's help file, accessible inside Neovim by running the help command. The plugin is written in Lua and supports installation through popular plugin managers including lazy.nvim, packer.nvim, and vim-plug.

Copy-paste prompts

Prompt 1
Help me configure comment.nvim in my Neovim setup using lazy.nvim so gcc toggles line comments and gbc toggles block comments.
Prompt 2
How do I set up comment.nvim with treesitter so it uses the right comment style when my cursor is inside a script tag in an HTML file?
Prompt 3
Write a comment.nvim pre_hook function that adds proper JSX comment support so React component files use the correct syntax.
Prompt 4
Show me all the visual-mode key mappings comment.nvim provides so I can add them to my Neovim cheat sheet.
Open on GitHub → Explain another repo

← numtostr on gitmyhub — every repo by this author, as a profile.

Verify against the repo before relying on details.