128

How do I comment multiple lines in Clojure?

Felipe Augusto
  • 7,733
  • 10
  • 39
  • 73
unj2
  • 52,135
  • 87
  • 247
  • 375

10 Answers10

158

Actually, there is a way!


(comment

(defn hey [] ("Hey there!"))

Check me out! )

Just wrap your comments in (comment ..) :)

Have fun!

Rayne
  • 31,473
  • 17
  • 86
  • 101
  • 4
    I thought id miss my #||# but this is way better. – unj2 Jul 28 '09 at 02:35
  • 4
    I hope you're having fun with Clojure. :) – Rayne Jul 28 '09 at 02:37
  • 15
    A word of caution: it seems like whatever is inside a (comment ...) needs to be a proper form; e.g. (comment hello :world) is fine but (comment hello: world) yields an exception. [Edit:] It seems like I should have read the answer by Greg Hewgill before posting this comment... oh well, I'll leave it anyway in case someone does the same thing I did. – paul Jul 07 '11 at 00:39
  • 21
    A word of warning -- the (comment) macro expands to nil. Use #_ to comment a single form, or #_(comment ...) to comment multiple forms without inserting a nil. – Tim has moved to Codidact Dec 18 '11 at 02:55
  • 1
    It remembers me this macro: http://stackoverflow.com/questions/3732173/multiple-lines-comments-in-scheme-rnrs – Felipe Aug 27 '14 at 01:46
  • Didn't work for me - because my block I wanted to comment was inside an if and still counted as an argument. #_ method below is better – Robert3452 Oct 07 '17 at 14:43
109

Clojure supports a #_ reader macro which completely skips the next form. This is mentioned on the page about the Clojure Reader. There is also the comment macro which has a similar effect, but is implemented differently.

Both the above require that the thing that you're commenting out is otherwise a syntactically correct S-expression.

Some Lisp dialects have a multi-line comment that can contain arbitrary text, but I don't see one for Clojure.

Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285
  • 22
    A Clojure multi-line arbitrary-text comment form does not exist. I submitted a patch adding #|......|# comments, but it was rejected as not a desired feature. So it's unlikely to get added anytime soon. – amalloy Apr 12 '11 at 02:07
  • 7
    @amalloy: What was the rationale? Just not needed, or was there an objection? – Ted Mar 26 '13 at 03:02
  • 2
    I often write lengthy comments in an imitation of literate programming style. I just use emacs to re-flow multiline comments (in the `*scratch*` buffer, just type some junk after `;` `;;` or `;;;` on multiple lines, put the cursor on the first word after the semicolons, and press `Meta-Q`). – Reb.Cabin May 30 '13 at 17:20
  • You can use `#_"` to open a multi-line free form text comment and close it with `"`. The only gotcha is you will need to escape double quotes within it. – Rachel K. Westmacott Sep 15 '22 at 13:21
  • N.B. you can also stack `#_`s. So `#_#_#_` comments out the next three forms. – Rachel K. Westmacott Sep 15 '22 at 13:22
29

Double quotes (string literal) allow adding arbitrary text (not only proper S-forms):

(comment "

public class HelloWorld {
    public static void main(String[] args) {
        System.out.print("Hello, World");
        System.out.println();
    }
}

")
Ivan Sviatenko
  • 687
  • 9
  • 8
  • 4
    nice trick that worked for me! But how is it possible the quotes on "Hello World" don't need to be escaped? – marathon Mar 02 '17 at 22:37
  • 9
    It's just luck. Ivan's example parses into 4 forms: The first form is a string, the second form is the symbol `Hello`, the 3rd form is the symbol `World`, and the 4th form is another string. `comment` just ignores all the forms. So in the example we're lucky that `Hello, World` parses as legal clojure. It doesn't work with arbitrary text. For example, `(comment "print("/foo")")` will die with the error `Invalid token: /foo`. – John Wiseman May 12 '17 at 20:50
18

Other examples are great, I'd just like to add one more trick:

Sometimes you want to comment out a few lines of code, but still have the compiler compile it and report any errors (e.g. a set of commands in a top-level namespace that you plan to execute later at the REPL).

In this case I like to wrap the code with (fn [] .....) which means that it still gets compiled, it just doesn't get called.

mikera
  • 105,238
  • 25
  • 256
  • 415
15

See this link: http://en.wikibooks.org/wiki/Clojure_Programming/Tutorials_and_Tips

You can create multiline comments with the syntax

(comment .....
    ....)
chollida
  • 7,834
  • 11
  • 55
  • 85
9

When using emacs and cider, there's a command M-x comment-region which I use often.

claj
  • 5,172
  • 2
  • 27
  • 30
3

There are multiple ways that I know:

First one is using the comment macro: it only does not evaluates all the code inside the comment body (but it still checks for balanced parenthesis/brackets). If you know your way with paredit, it won't take much time if you want to comment a few sexp calls.

(comment 
 (println 1))

However, it will still check for parenthesis match. So if you have unbalanced parenthesis, you code won't compile (yielding java.lang.RuntimeException: EOF while reading).

Another way is using #_ (aka the discard macro): it will discard the next sexp, which is the way I personally prefer (faster to type and normally I do that on sexps when I have to debug):

#_(println 1)

It also checks for unmatched delimiters: so if you have unbalanced parenthesis, it won't compile as well.

Lastly, there is the ; character which will comment the line (similar to the other languages commentary feature) and the compile will ignore it completely. If you want to comment multiple lines, you need to prepend all the lines with ; , which are normally a hassle, but usually text editors will do it for you with a command after selecting multiple lines.

;  (println 1)
;  (println 1 also won't break
Rodrigo Flores
  • 2,411
  • 18
  • 17
2

In Emacs you can use the command M-; (the shortcut for M-x comment-dwim). It will comment or uncomment any marked region of text/code, so, as long as your entire function or set of functions is included in the region, you can comment or uncomment that region quite easily. The Emacs reference manual for the function M-; can be found here.

MLev
  • 421
  • 1
  • 6
  • 16
0

For a long comment block, Macros #_ or (comment ...) didn't work as proprietarily, then I did comment block with VSCODE(OS X).

  1. Highlight the comment block.
  2. press command+shift+p in vscode.
  3. find "Add Line Comment"
madeinQuant
  • 1,721
  • 1
  • 18
  • 29
0

Clojure also supports a double #_ #_ reader macro which completely skips the next two forms. Very useful for commenting out a key and multi-line value in a map.

Example

{
:db-connector "jdbc"
#_ #_ :auto-offset-reset    {:dev     "earliest"
                             :default "latest"}
}
CambodianCoder
  • 467
  • 4
  • 14