83

I'm aware that I can use (* ... *) to comment out stuff in a notebook. Many languages have a syntax for single-line comments, such as

  • // in C, C++, Java, C#, ...
  • # in shell, Python, ...

For example, in C++ you can write

int x = 5;
// std::cout << "debug: x = " << x << std::endl;
return x;

in which the second line is commented out.

Is there a way to quickly comment out just one line in Mathematica, without having to type paired comment characters?

AsukaMinato
  • 9,758
  • 1
  • 14
  • 40
Peeter Joot
  • 6,398
  • 4
  • 36
  • 55
  • 10
    if you select the line and press ctrl-/ it gets commented out automatically (as in many IDEs) – acl Feb 08 '12 at 14:48
  • 3
    Is it Ctrl+/ or Alt+/ (see Szabolcs answer)? – Yves Klett Feb 08 '12 at 16:51
  • @Yves it's cmd-/ on my mac, and I thought I remembered it being ctrl-/ elsewhere. Clearly I was wrong (I have not driven my big machine at work from its keyboard for a long time so have no idea of the shortcuts there). – acl Feb 08 '12 at 18:56
  • Would it be useful to create a tag for these cross-system issues? – Yves Klett Feb 09 '12 at 07:39
  • 2
    Just enclose the line between (* and *) –  Aug 15 '14 at 15:39
  • No way to comment out a line. Best way if you would add a comment is to create a "plain text" cell and put your comments there. See: https://reference.wolframcloud.com/cloudplatform/workflow/CreateATextCell.html#cloud-tab – imz22 Dec 13 '22 at 03:37

5 Answers5

86

There is no way to comment out a single line.

Mathematica doesn't really respect lines, it pushes working at the expression level when possible (not at the source text level). Converting cells between different forms (StandardForm, InputForm) will even shuffle around newlines. Copying and pasting code does the same.

As @acl has mentioned, you can select a piece of code and comment it out with Alt-/. The shortcut Ctrl-. makes it easy to select subparts of expressions. These commands are found in the Edit menu (mentioning in case the keyboard shortcut is different on other platforms).

I am not advocating this behaviour, just explaining the current situation.

dionys
  • 4,321
  • 1
  • 19
  • 46
Szabolcs
  • 234,956
  • 30
  • 623
  • 1,263
  • I didn't know about these keyboard shortcuts before... thanks! It's especially helpful to me that Alt+/ toggles commenting. – J. M.'s missing motivation Feb 08 '12 at 15:29
  • 2
    On my german keyboard the Ctrl/Alt+. shortcut does not work. This is the case for a few of the standard Mathematica bindings. – Yves Klett Feb 08 '12 at 16:50
  • @Yves What OS do you use? You can check in the Edit menu what the shortcut is on your system. Feel free to edit my answer if it is different on your system ("community wiki" = feel free to edit). – Szabolcs Feb 08 '12 at 16:53
  • @Szabolcs: Here on Windows 7 64bit with german language and keyboard. It says Alt+/ in the menu all right, and if I choose this entry via mouse, it does the deed - but if I type Alt+/ (which on my keyboard results in Alt+Shift+7 (see link german keyboard layout) ) the cell is converted to Text (which is usually Ctrl+7). So either my system is messed up or it is a bug or consequence of the german layout. – Yves Klett Feb 08 '12 at 17:02
  • @Yves try Alt-[-] (that is alt-dash). The key next to the right shift. Curious if it works. I only use US layout for everyday work and programming, and switch to alternate layouts for typing in other languages ... partly for this reason (i.e. things breaking with other layouts) ... that's how I ended up with 6 layouts – Szabolcs Feb 08 '12 at 17:19
  • @Szabolcs: no joy there. Strangely this diminishes the font size each time for the next characters. Kind of surprising. So keep away from german layouts if you are going for the all-keyboard way of coding. – Yves Klett Feb 08 '12 at 17:43
  • @Yves I just switched my keyboard (on OS X) to German layout and it does the exact same thing as yours. – acl Feb 08 '12 at 19:00
  • Actually, you can comment-out a line that doesn't form a complete expression. For example, in

    Plot[x, {x,0,1}] </code

    with the mouse select the entire first line. Then press Cmd+/ (on a Mac) and the first line will be commented out -- leaving, of course, a syntax error showing on the uncommented 2nd line.

    – murray Feb 08 '12 at 20:08
  • @acl: Thanks for the reality check, now I am feeling a lot less loony. If this occurs even on OS X then it is some kind of consistent (mis)behaviour. – Yves Klett Feb 09 '12 at 07:38
  • @Szabolcs can you comment on why you don't advocate this behavior? I like to comment out the Export[] line for a plot that I'm revising until I want to commit to exporting, possibly accidentally, the revised plot. – ArgentoSapiens Feb 09 '12 at 18:07
  • 4
    @YvesKlett I'm using a german keyboard too and I found a way to edit the shortcuts, so you can use them with the germany keyboard layout. You have to edit a text file in Mma's installation folder. I think it was mentioned here on SE in an other topic too. EDIT: here's the link http://stackoverflow.com/questions/5604837/keyboard-shortcut-to-un-comment-out-code-in-mathematica-7 – Phab Apr 11 '14 at 06:26
14

Also, for the mouse-oriented user, you can select an expression, call up the context menu by right mouse click and choose "Un/Comment".

Yves Klett
  • 15,383
  • 5
  • 57
  • 124
  • 1
    To me this is the most natural way since I am already selecting the text with the mouse. (* BTW lustig, dass hier so viele eine deutsche Tastatur haben ...*) – Dr. Wolfgang Hintze Jun 16 '21 at 08:17
  • This solution is very useful to me because I use to like comment short commands in the same line, especially when I use a semi-column to not show the result. I write a comment, select it and use the U/Comment from context menu. – ALEXANDRE ARARIPE Sep 29 '21 at 17:37
10

To "comment out" a complete cell you also can just make it non-evaluatable: Select the cell and use the menu checkbox item "Cell|Cell Properties|Evaluatable".

celtschk
  • 19,133
  • 1
  • 51
  • 106
4

Actually, you can comment-out a line that doesn't form a complete expression. For example, in

   Plot[x,

   {x,0,1}]

select the entire first line by dragging the mouse over it. Then press Cmd+/ (on a Mac) and the first line will be commented out -- leaving, of course, a syntax error showing on the uncommented 2nd line.

acl
  • 19,834
  • 3
  • 66
  • 91
murray
  • 11,888
  • 2
  • 26
  • 50
2

There is now auto-matching of delimiters:

https://reference.wolfram.com/language/workflow/EnterPairedDelimiters.html

So if you type (* you automatically get (* *) with the mouse correctly positioned to insert your comment.

TheDoctor
  • 2,832
  • 1
  • 14
  • 17