Comment Boxes

Sometimes it is useful to break up blocks of your writing visually with comment boxes. I use these to separate out major sections of e.g. a piece of code. To do this, highlight the lines of text that you want to enclose in a comment box, and then use M-x comment-box.

The example below illustrates this, with the first comment box showing the default behaviour. I prefer a wider comment box, as in the second box below.


To get the boxes in the second example, add the flowing code to your emacs config file (copied from irreal). This defines a function that you can run with M-x bjm-comment-box and sets up a keyboard short-cut C-c b b.

;; Full width comment box                                                 ;;
;; from                                     ;;
(defun bjm-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)
  (insert-char ?  (- fill-column (current-column)))
  (comment-box b e 1)
  (goto-char e)
  (set-marker e nil)))

(global-set-key (kbd "C-c b b") 'bjm-comment-box)
  • stefanavey

    Love this! This beats hands down my current method of typing out many comment characters, killing, and yanking repeatedly. Thanks for all the handy tips!