Tell me more ×
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. Is there a way to quickly comment out just one line without paired comment chars?

share|improve this question
5  
if you select the line and press ctrl-/ it gets commented out automatically (as in many IDEs) – acl Feb 8 '12 at 14:48
1  
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

4 Answers

up vote 18 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
1  
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
@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 8 '12 at 17:19
show 5 more comments

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

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.

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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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