comment-dwim-2
is a replacement for the Emacs' built-in command comment-dwim
, which includes more features and allows you to:
- comment/uncomment the current line (or region, if active)
- insert/kill end-of-line comments
- reindent end-of-line comments
comment-dwim-2
picks one behavior depending on the context but can also be repeated several times to switch between the different possible behaviors.
comment-dwim-2
used 3 times in a row:
Install this package from MELPA.
Then, add your own key binding, for instance:
(use-package comment-dwim-2
:bind
("M-;" . comment-dwim-2))
or
(keymap-global-set "M-;" #'comment-dwim-2)
comment-dwim-2
works as expected inside org-mode code blocks. In regular org-mode content, it fallbacks to org-toggle-comment
:
When commenting a region, comment-dwim-2
will by default comment the entirety of the lines that the region spans (i.e. a line will be fully commented even if it is partly selected):
In Lisp modes, however, comment-dwim-2
will strictly comment the region as commenting whole lines could easily lead to unbalanced parentheses.
If you always want to fully comment lines (Lisp modes included), add this to your configuration file:
(setopt comment-dwim-2-region-function #'comment-dwim-2-comment-or-uncomment-lines)
If you only want to comment the selected region (like comment-dwim
does), add this:
(setopt comment-dwim-2-region-function #'comment-dwim-2-comment-or-uncomment-region)