Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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?

share|improve this question
if you select the line and press ctrl-/ it gets commented out automatically (as in many IDEs) –  acl Feb 8 '12 at 14:48
Is it Ctrl+/ or Alt+/ (see Szabolcs answer)? –  Yves Klett Feb 8 '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 8 '12 at 18:56
Would it be useful to create a tag for these cross-system issues? –  Yves Klett Feb 9 '12 at 7:39
Just enclose the line between (* and *) –  Vincenzo Aug 15 '14 at 15:39

4 Answers 4

up vote 33 down vote accepted

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 menioned, 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.

share|improve this answer
I didn't know about these keyboard shortcuts before... thanks! It's especially helpful to me that Alt+/ toggles commenting. –  J. M. Feb 8 '12 at 15:29
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 8 '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 8 '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 8 '12 at 17:02
@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/… –  Phab Apr 11 '14 at 6:26

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

share|improve this answer

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

share|improve this answer
This will be a perfect hoax if someone doesn't know this cell property :-D –  luyuwuli Apr 11 '14 at 3:01

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



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.

share|improve this answer

protected by Mr.Wizard Aug 15 '14 at 15:48

Thank you for your interest in this question. Because it has attracted low-quality answers, posting an answer now requires 10 reputation on this site.

Would you like to answer one of these unanswered questions instead?

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