What is the preferred way to comment out sexps in elisp code? I have been wrapping my sexps in (if nil ...)
so far.
3 Answers
C-M-@ M-;
comments current sexp.
C-M-@
stays for mark-sexp
and M-;
knows how to correctly comment out a region, taking the current mode into account. Commands to mark objects are described here.

- 1,117
- 6
- 11
-
4`C-M-@` may also be bound to `C-M-SPC` which may be an easier to type and remember. – kristianlm Sep 03 '12 at 11:02
Your (if nil sexp)
construct will work just fine. I don't know if there is a standard equivalent in Emacs Lisp, though I suspect not. If you want to be more explicit about what you are doing, you could use something like the comment macro in clojure. Which is easily implemented as an Emacs Lisp macro.
(defmacro comment (&rest body)
"Comment out one or more s-expressions."
nil)
Then you could just write:
(comment
...
; As many sexps as you want here...
...)
EDIT:
Added nil
to the end of the definition of the comment
macro. As pointed out by Marko Topolnik, if you don't provide an implementation body, defmacro
assumes that the docstring is actually the body. Explicitly putting a nil
at the end works around this. Thanks for the catch Marko!
-
3The macro is nice because you can have multiple sexps, whereas the if only works for a single sexp. An obvious alternative would be (when nil ....) – Trey Jackson Nov 17 '10 at 02:56
-
Caveat: the macro only works for top-level forms. It expands to what the author apparently intended to be its doc string. – Marko Topolnik Jan 07 '13 at 10:45
semicolons -- ;;;
;;; insert into buffer
(defun thingy (foo)
(interactive "stallman: ") ; prompt the user
; one semi-colon is also enough
;;;(insert (concat "deprecated")) ;;; this line will not execute
(insert (concat "gnu-" foo)))

- 9,020
- 5
- 48
- 68
-
1Thanks, but semicolons will comment out whole lines. I was looking for a way to comment out individual sexps. – sigjuice Nov 17 '10 at 02:29
-
3I rather doubt that you can comment out a sexp without using the language's comment mechanism. `(if nil sexp)` returns a value of nil, whereas a comment is ignored and has no value. As such, I would be a little wary of using the term "comment" to describe this, because that's not what it is. – phils Nov 17 '10 at 03:21
-
yeah, I was fixating on "comment".... which are applied from the semicolon(s) to EOL – Michael Paulukonis Nov 18 '10 at 20:43