A replacement for the emacs' built-in command `comment-dwim'
Emacs Lisp
Latest commit 8cedecd Aug 26, 2015 Rémy Ferré Use cl-lib.el instead of cl.el
Permalink
Failed to load latest commit information.
README.md Fix typo Nov 14, 2014
comment-dwim-2-test.el Code review Sep 19, 2014
comment-dwim-2.el Use cl-lib.el instead of cl.el Aug 25, 2015

README.md

Comment-dwim-2

comment-dwim-2 is a replacement for the Emacs' built-in command comment-dwim which includes more comment features, including:

  • commenting/uncommenting the current line (or region, if active)
  • inserting an inline comment
  • killing the inline comment
  • reindenting the inline comment

As its name suggests, comment-dwim-2 picks one behavior depending on the context but contrary to comment-dwim can also be repeated several times to switch between the different behaviors.

Demo

comment-dwim-2 repeated 3 times:

general behavior of comment-dwim-2

How to use

comment-dwim-2 is not bound to any key, so you need to set up you own keybinding first. For instance:

(global-set-key (kbd "M-;") 'comment-dwim-2)

Installation

This package can be installed from MELPA.

Detailed use cases

Commenting/uncommenting the region

commenting/uncommenting the region with comment-dwim-2

Commenting current line

commenting current line with comment-dwim-2

Uncommenting current line

uncommenting current line with comment-dwim-2

Insert comment (repeated twice)

inserting comment with comment-dwim-2

Kill comment (repeated twice)

killing comment with comment-dwim-2

Reindent comment (called with a prefix argument)

reindenting comment with comment-dwim-2

Customization

An alternative behavior closer to what comment-dwim does is available. To use it, add this to your init file:

(setq comment-dwim-2--inline-comment-behavior 'reindent-comment)

It basically swaps the killing and reindenting behavior, which means that repeating comment-dwim-2 will by default reindent the comment instead of killing it, and that calling comment-dwim-2 with a prefix argument will kill the comment instead of reindenting it.