Emacs Comment Functions

Bozhidar Batsov over at Emacs Redux has taken some time off from his open source projects such as rubocop, nrepl, cider, prelude, projectile, and many others to write a useful post on the various Emacs commenting commands. It turns out that there are a lot of them but like most of you, I long ago settled on comment-dwim (bound to Meta+;) because I thought it handled all my normal commenting needs more or less automatically. It was nice to need to remember only one command that handles everything.

I said “thought it handled” rather than “think it handles” because Batsov’s post mentioned another command I didn’t know about: comment-line. It handles all the cases I care about that comment-dwim does but also handles deleting the comment on the current line, which is a bit clumsy with comment-dwim. Batsov says he much prefers comment-line and has changed the Meta+; binding to run it instead.

The other commenting command that I occasionally find useful is comment-box. I like to use it for section headers or just before C functions. What I don’t like about it is that the width of the box is scaled to fit the comment width. I prefer to have it spread across the page width. I wrote about that here but the code is short so here it is again in case anyone is interested:

(defun jcs-comment-box (b e)
  "Draw a box comment around the region but arrange for the region
to extend to at least the fill column. Place the point after the
comment box."
  (interactive "r")
  (let ((e (copy-marker e t)))
    (goto-char b)
    (end-of-line)
    (insert-char ?  (- fill-column (current-column)))
    (comment-box b e 1)
    (goto-char e)
    (set-marker e nil)))

As Batsov says, comments are an important part of programming and it’s worth learning the tools that Emacs provides to make using them easier. Take a look at his post to see what’s available.

This entry was posted in General and tagged . Bookmark the permalink.