Skip to content

remyferre/comment-dwim-2

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

52 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Comment-dwim-2

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.

Demo

comment-dwim-2 used 3 times in a row:

general behavior of comment-dwim-2

Setup

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)

Detailed use cases

Comment/uncomment the region

commenting/uncommenting the region with comment-dwim-2

Comment current line

commenting current line with comment-dwim-2

Uncomment current line

uncommenting current line with comment-dwim-2

Insert comment (repeat the command)

inserting comment with comment-dwim-2

Kill comment (repeat the command)

killing comment with comment-dwim-2

Reindent comment (call the command with a prefix argument)

reindenting comment with comment-dwim-2

Org-mode support

comment-dwim-2 works as expected inside org-mode code blocks. In regular org-mode content, it fallbacks to org-toggle-comment:

using comment-dwim-2 in org-mode

Customization

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):

comment-dwim-2 comment lines even if they are partly selected

In Lisp modes, however, comment-dwim-2 will strictly comment the region as commenting whole lines could easily lead to unbalanced parentheses.

region commenting in Lisp modes

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)

About

A replacement for the emacs' built-in command `comment-dwim'

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 3

  •  
  •  
  •