1

How can I make a full line of code into a comment by typing a single character inserted at start of the line?

In other programming languages I can use characters such as ! , # and language processor will ignore what follows to next newline. I think using (* ... *) is a bit shoddy because it takes six keystrokes.

m_goldberg
  • 107,779
  • 16
  • 103
  • 257
Narasimham
  • 3,160
  • 13
  • 26
  • 2
    What exactly is a line ?:D – Dr. belisarius Aug 05 '15 at 17:38
  • It is what it is. Use a keyboard macro if typing causes such grief. Use a different language if even the thought of it troubles you. – ciao Aug 05 '15 at 17:40
  • @bel, depends. How wide is your monitor? ;) – J. M.'s missing motivation Aug 05 '15 at 17:45
  • 2
    I guess you don't know this: select something, press Alt + /, and presto! – J. M.'s missing motivation Aug 05 '15 at 17:47
  • @Guesswhoitis. In a more serious mood, this also shook me when I was starting with Mma. Discovering that there are no line nor delimiter concepts made a mind blowing day then. – Dr. belisarius Aug 05 '15 at 17:50
  • Exempting any data or text of one code line from execution with a single start character like( ! ) is common in several softwares, I thought. – Narasimham Aug 05 '15 at 18:16
  • @Narasimham Mathematica is unique in many ways. Some of them are sketched here, just in case you haven't seen it – Dr. belisarius Aug 05 '15 at 18:19
  • But, as already noted, since Mathematica has no concept of lines per se, you cannot have a comment through a unary switch; you need to enclose whatever it is you want to comment out into something. – J. M.'s missing motivation Aug 05 '15 at 18:19
  • Actually, it doesn't seem to be completely transparent. E.g. 1 [return] 2 assigns In[1]:=1 and In[2]:=2, so a linebreak is respected. However, 3[4, [return] 5] assigns In[3]:=3[4,5], so here it isn't. It seems, though, for all intents and purposes, where a line is handled as a "separate line" it is also a separate input. – LLlAMnYP Aug 05 '15 at 18:32
  • 1
    I extremely rarely type in (* *), but use right-click Un/Comment. – Karsten7 Aug 05 '15 at 18:40
  • @LLlAMnYP, as a note, that is actually relatively new behavior (although exactly when that was introduced escapes me). Many an old hand forgot to put a semicolon where they should've and were subsequently mystified by errors saying they multiplied two things they shouldn't have, or have made a recursive definition, or a number of other related errors. – J. M.'s missing motivation Aug 05 '15 at 18:42

1 Answers1

5

Mathematica does not support any kind of comment-to-line-end specaial character(s). You will have to resign yourself to living with that. It does support a very useful and easy to apply block un/commenting feature.

I am running on OS X, so the keyboard shortcut for the context (mouse-right-click) menu's Un/Comment command is Cmnd+/. I use that do any kind of line or block un/commenting. Here is how it works.

Suppose I have a line that looks this this

uncommented

When I click anywhere in the line sufficiently many times (three times will do it if I click on data) and type Cmnd+/, it changes to

commented

When I triple-click on the left-most parenthesis three-times, it reverts back to

uncommented

m_goldberg
  • 107,779
  • 16
  • 103
  • 257