78
$\begingroup$

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?

$\endgroup$
5
  • 9
    $\begingroup$ if you select the line and press ctrl-/ it gets commented out automatically (as in many IDEs) $\endgroup$
    – acl
    Feb 8, 2012 at 14:48
  • 3
    $\begingroup$ Is it Ctrl+/ or Alt+/ (see Szabolcs answer)? $\endgroup$
    – Yves Klett
    Feb 8, 2012 at 16:51
  • $\begingroup$ @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). $\endgroup$
    – acl
    Feb 8, 2012 at 18:56
  • $\begingroup$ Would it be useful to create a tag for these cross-system issues? $\endgroup$
    – Yves Klett
    Feb 9, 2012 at 7:39
  • 2
    $\begingroup$ Just enclose the line between (* and *) $\endgroup$
    – user19179
    Aug 15, 2014 at 15:39

4 Answers 4

80
$\begingroup$

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.

$\endgroup$
11
  • $\begingroup$ I didn't know about these keyboard shortcuts before... thanks! It's especially helpful to me that Alt+/ toggles commenting. $\endgroup$ Feb 8, 2012 at 15:29
  • 2
    $\begingroup$ On my german keyboard the Ctrl/Alt+. shortcut does not work. This is the case for a few of the standard Mathematica bindings. $\endgroup$
    – Yves Klett
    Feb 8, 2012 at 16:50
  • $\begingroup$ @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). $\endgroup$
    – Szabolcs
    Feb 8, 2012 at 16:53
  • $\begingroup$ @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. $\endgroup$
    – Yves Klett
    Feb 8, 2012 at 17:02
  • 4
    $\begingroup$ @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 stackoverflow.com/questions/5604837/… $\endgroup$
    – Phab
    Apr 11, 2014 at 6:26
12
$\begingroup$

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

$\endgroup$
2
  • 1
    $\begingroup$ 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 ...*) $\endgroup$ Jun 16, 2021 at 8:17
  • $\begingroup$ 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. $\endgroup$ Sep 29, 2021 at 17:37
9
$\begingroup$

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".

$\endgroup$
1
  • 1
    $\begingroup$ This will be a perfect hoax if someone doesn't know this cell property :-D $\endgroup$
    – luyuwuli
    Apr 11, 2014 at 3:01
5
$\begingroup$

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.

$\endgroup$

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