r/vim • • Feb 12 '26

Plugin I made a powerful vim9 commenting plugin

Post image

Comentador

A Vim9script plugin for toggling both inline and block comments with full operator-pending support. While inspired by tpope's Commentary plugin, Comentador has its own unique behavior and features. Key differences being everything is a toggle, single blank line auto-insert, and not being able to comment an already commented line. There are even more differences of course!

Requirements

  • Vim 9.0 or higher.

Documentation

Use the :help Comentador command for a complete documentation of usage and behavior.

Default Mappings

ModeMappingAction
NormalgccToggle on comment [count] lines / Toggle off any comment type [count] lines
Normalgc{motion}Toggle on comments over {motion} / Toggle off any comment type over {motion}
NormalgcuToggle off contiguous comments / Toggle on comment
NormalgbbToggle on block comment [count] lines / Toggle off block comments [count] lines
Normalgb{motion}Toggle on block comment over {motion} / Toggle off block comments over {motion}
NormalgbuToggle off contiguous inline-block comments / Toggle on inline-block comment
VisualgcToggle on comments for selection / Toggle off any selected inline type comments
VisualgbToggle on block comment for selection / Toggle off any selected block type comments

Use gcc to uncomment any comment type.

Use gbb when you specifically want to comment or uncomment block style comments.

Text Object Commands

The gc and gb mappings work as text objects with other operators:

CommandAction
dgcDelete any contiguous inline or single block comments
cgcChange any contiguous inline or single block comments
ygcYank any contiguous inline or single block comments
dgbDelete contiguous inline-block or single block comments
cgbChange contiguous inline-block or single block comments

Blank lines adjacent to comment blocks are included in the selection. With d or y, leading blank lines are trimmed but trailing blank lines are preserved. With c, blank lines are trimmed from both ends.

Command-line Commands

CommandAction
:[range]ComentadorToggle on comments [range] / Toggle off any comment type [range]
:[range]ComentadorBlockToggle on block comments [range] / Toggle off block comments [range]

Without a range, commands operate on the current line.

Plug Mappings

Override default mappings using <Plug> mappings:

Plug MappingDefaultMode
<Plug>(Comentador)gcNormal, Visual, Operator-pending
<Plug>(ComentadorLine)gccNormal
<Plug>(ComentadorBlock)gbNormal, Visual, Operator-pending
<Plug>(ComentadorBlockLine)gbbNormal

Example:

nnoremap <leader>c  <Plug>(Comentador)
nnoremap <leader>cc <Plug>(ComentadorLine)
xnoremap <leader>c  <Plug>(Comentador)
onoremap <leader>c  <Plug>(Comentador)

Comment Markers

Markers are automatically parsed from 'commentstring' and 'comments' options and cached in b:comentador_markers. For unsupported filetypes, set 'commentstring' for inline comments. If block markers are missing (no s1 and ex flags in 'comments'), add them to the existing value:

autocmd FileType apache setlocal commentstring=#\ %s
autocmd FileType myfile setlocal comments+=s1:/*,ex:*/

If no comment format is defined for a filetype, all mappings will display "No comment format defined for this filetype".

139 Upvotes

24 comments sorted by

View all comments

22

u/Biggybi Gybbigy Feb 12 '26 edited Feb 12 '26

and not being able to comment an already commented line

I'm not sure I like that behaviour. Sometimes, when testing, I just want to add another level of comment. Makes it easier to uncomment what I commented in the previous step.

But that might be a weird idea on my end. 

Your plugin looks very good.

7

u/Audiofile48 Feb 12 '26

i tend to do the same

5

u/shleebs Feb 12 '26

It allows you to wrap inline comments in a block comment, so in that way it's still possible. I personally don't like doubling or tripling up on inline comment markers, so the behavior definitely matches my preference. Maybe i'll add a config option to change that 🤔