First, yes, it's true that Emacs Lisp does not have a real block-comment syntax. This is different from Common Lisp, which has #|
... |#
.
That said, you can use ;
-style comments to get much of the effect of #|
...|#
commenting, if not quite the same flexibility or appearance.
My answer is to use comment-region
, not comment-dwim
. IMO, the former is much more useful when you want to, for example, nest blocks of comments and, in particular, unnest a given level of nesting. I use comment-dwim
(M-;
) only for single-semicolon comments after a line of code.
For comment-region
:
A plain C-u
unnests a full level of comments, for the region.
A negative prefix arg removes that many comment chars. E.g., M- - 2
removes two ;
chars.
However, I actually prefer the following, comment-region-lines
from library misc-cmds.el
, because I typically want to comment/uncomment full lines as a block comment. I bind it to C-x C-;
.
(defun comment-region-lines (beg end &optional arg)
"Like `comment-region' (which see), but comment or uncomment whole lines."
(interactive "*r\nP")
(when (> beg end) (setq beg (prog1 end (setq end beg))))
(let ((bol (save-excursion (goto-char beg) (line-beginning-position)))
(eol (save-excursion (goto-char end) (if (bolp) (point) (line-end-position)))))
(comment-region bol eol arg)))
Here's the doc string of comment-region
, which the doc of comment-region-lines
refers to:
comment-region
is an interactive compiled Lisp function in
newcomment.el
.
(comment-region BEG END &optional ARG)
Comment or uncomment each line in the region.
With just C-u
prefix arg, uncomment each line in region BEG
.. END
.
Numeric prefix ARG
means use ARG
comment characters.
If ARG
is negative, delete that many comment characters instead.
The strings used as comment starts are built from comment-start
and comment-padding
; the strings used as comment ends are built
from comment-end
and comment-padding
.
By default, the comment-start
markers are inserted at the
current indentation of the region, and comments are terminated on
each line (even for syntaxes in which newline does not end the
comment and blank lines do not get comments). This can be
changed with comment-style
.