14

For example, if I want to comment this:

(defun noop ()
  nil)

Every time I try to put a semicolon before the "(defun", the defun runs away to the next line. So how is that supposed to be done?

GNU Emacs 23.1.1

Edit: by "running away" I mean when I insert a semicolon before "(defun", a newline is automatically inserted after the semicolon and "(defun" starts on a new line again.

4
  • 2
    It works for me, no running away here. Are you saying that when you insert a semicolon before "(defun", a newline is automatically inserted after it and "(defun" starts on a new line again? That would be very strange; it happens in neither of my emacs versions (GNU Emacs 22.1 and Aquamacs Emacs based on GNU Emacs 22.3.1).
    – joriki
    Feb 6, 2011 at 17:25
  • Yep, it happens like that. GNU Emacs 23.1.1.
    – Ron
    Feb 6, 2011 at 17:29
  • 2
    You have most likely paredit installed.
    – Svante
    Feb 6, 2011 at 17:33
  • So what mode did it turn out to be? Feb 7, 2011 at 15:35

8 Answers 8

16

See the command M-x comment-region and related.

7

M-X comment-dwim or M-;, which is the default key binding for the former — might save you a few key strokes, since it not only comments, but uncomments region, if it's commented already. Anyway, check out Emacs Manual for a proper description.

1
  • unfortunately on my emacs M-x comment-dwim inserts semicolon at the end of the line, M-; inserts three semicolons and a newline
    – Ron
    Feb 7, 2011 at 11:57
6

Ron, do a CTRL-H m and look at the minor modes. You have some "helpful" minor mode active. (Maybe paredit but I dont think that's it.) I remember there was something like that when I tried the EMACS Starter Kit. It lasted maybe thirty seconds before I screamed and found how to kill it.

In any case, that's not default EMACS behavior, it's some init-file or site-emacs addition.

1
  • 4
    Type C-h k ; and Emacs will tell you which command semicolon is bound to, and which library defines it. From there you should be able to figure out what you need to disable.
    – phils
    Feb 6, 2011 at 19:50
5

Mark both lines and call M-x comment-region. Also look at comment-or-uncomment-region and comment-dwim functions.

2

For the specific task you asked for in the headline (commenting a complete expression that may span multiple lines at once), first press C-M-SPC (bound to mark-sexp) to set the region to the expression following point, and then M-; (bound to comment-dwim which will call comment-region).

2

a little late to the party, however, what about:

(defmacro comment (&rest a))
3
  • 2
    Sad that it took a few years to be caught but these macro solutions will not be very useful because BODY will still need to evaluate without error.
    – ocodo
    Feb 8, 2019 at 8:46
  • @ocodo What do you refer to by BODY? In what way does the solution of this answer work differently from the comment macro in clojure.core?
    – Rulle
    Jan 4 at 7:36
  • 1
    @Rulle, BODY is the "content" of the comment. Ocodo is essentially right, the contents has to parse properly. This may not be the best solution for textual or instructional comments, however it is great for commenting out s-expressions. It is identical to clojure's definition, and they share this same limitation.
    – Shlomi
    Jan 9 at 17:53
1

If you're talking about Common Lisp (rather than, say, Emacs-Lisp), you can use #+(or):

#+(or)
(defun noop ()
  nil)

See the CLHS for details.

0
(defvar orgCmntEnd nil "Org Comment End")

(defun orgCmntBegin (<comment <commentEnd))

(orgCmntBegin "
**  orgCmntBegin. Permits us to include * at the beginning of line as a comment.
Which in turn allows us to switch between emacs-major mode and org-mode for COMEEGA.
Example usage is: 
(orgCmntBegin \"multi-line comment comes here.\" orgCmntEnd)

I wish elisp had a here-document facility. Like common-lisp.
Anybody listening?
" orgCmntEnd)
4
  • Please add further details to expand on your answer, such as working code or documentation citations.
    – Community Bot
    Sep 8, 2021 at 1:24
  • That is working code. All 3 forms can be eval-ed. Sep 9, 2021 at 5:26
  • You can put the first two forms in a library. And the 3rd form demonstrate usage. The beauty of the whole thing is that block comments -- as strings -- are well delineated and the block looks pretty. orgCmntBegin is an empty function whose doc-string is the comment block and orgCmntEnd is just a variable that makes usage more obvious. Sep 9, 2021 at 5:35
  • Last sentence of previous comment should have been: orgCmntBegin is an empty function which is invoked by a string which contains the comment block and the second argument is orgCmntEnd which makes the usage more obvious. Sep 9, 2021 at 6:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.