r/vim • • Feb 12 '26

Plugin I made a powerful vim9 commenting plugin

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

Mode Mapping Action
Normal gcc Toggle on comment [count] lines / Toggle off any comment type [count] lines
Normal gc{motion} Toggle on comments over {motion} / Toggle off any comment type over {motion}
Normal gcu Toggle off contiguous comments / Toggle on comment
Normal gbb Toggle on block comment [count] lines / Toggle off block comments [count] lines
Normal gb{motion} Toggle on block comment over {motion} / Toggle off block comments over {motion}
Normal gbu Toggle off contiguous inline-block comments / Toggle on inline-block comment
Visual gc Toggle on comments for selection / Toggle off any selected inline type comments
Visual gb Toggle 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:

Command Action
dgc Delete any contiguous inline or single block comments
cgc Change any contiguous inline or single block comments
ygc Yank any contiguous inline or single block comments
dgb Delete contiguous inline-block or single block comments
cgb Change 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

Command Action
:[range]Comentador Toggle on comments [range] / Toggle off any comment type [range]
:[range]ComentadorBlock Toggle 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 Mapping Default Mode
<Plug>(Comentador) gc Normal, Visual, Operator-pending
<Plug>(ComentadorLine) gcc Normal
<Plug>(ComentadorBlock) gb Normal, Visual, Operator-pending
<Plug>(ComentadorBlockLine) gbb Normal

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".

140 Upvotes

24 comments sorted by

22

u/Biggybi 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.

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 🤔

7

u/Audiofile48 Feb 12 '26

i tend to do the same

6

u/tremby Feb 12 '26

I don't like how when you comment multiple lines at once the comment leaders of each line are put at different indentation levels. (Near the start of your demo.)

I want to see "this chunk of code was commented out all at once" signposted by the comment leaders all having the same level of indentation.

2

u/shleebs Feb 12 '26

Personally if I need that kind of signpost, I use block comments. However adding a configuration option to support same level indentation for inline comments is possible. Would you prefer the comment markers at the very begining of the line if there was such a config option?

→ More replies (3)

7

u/Iskhartakh Feb 12 '26

You can do :packadd comment and use select and 'gc' to comment. Comment style will adjust to filetype automatically.

2

u/shleebs Feb 12 '26

This behaves differently with additional behavior, which is why I made it

1

u/porfiriopaiz Feb 12 '26

What did you use to add the text or keystrokes on screen?

2

u/Iwisp360 Feb 13 '26

gcc -> GNU Compiler Collection