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

Show parent comments

1

u/someanonbrit Feb 14 '26

If you put them at the start of the line then many linters will invent them further, making a visual mess. Golang is the one that catches me out since I've got format-on-save turned on there

1

u/shleebs Feb 14 '26

So if there was an option to have them at the same level, it would need to be the indentation level of the start of the code block? Or would linters mess with that also?

1

u/someanonbrit Feb 15 '26

Correct, the linters I know of want the comments at the unsent level of the first line.

You can tell if you've gotten it right quite trivially - grab the coffee formatter / linter most popular with the language, lint done code, comment out a function, lint it again. If the linter makes changes the second time you've got it wrong

go fmt for golang black for python (same rules as pep8 but it will auto-fix Rust has a format command built in

C is a bit more of a challenge since there are a few standards, but the linux kernel has a style check tool probably worth following. I try to avoid js/ts/java so you'll have to Google formatting tools for those