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:
You need to add your own key binding first, for instance:
(global-set-key (kbd "M-;") 'comment-dwim-2)
If you use org-mode, consider using org-comment-dwim-2
to comment within source blocks:
(define-key org-mode-map (kbd "M-;") 'org-comment-dwim-2)
This package can be installed from MELPA.
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:
(setq cd2/region-command 'cd2/comment-or-uncomment-lines)
If you only want to comment the selected region (like comment-dwim
does), add this:
(setq cd2/region-command 'cd2/comment-or-uncomment-region)