1
$\begingroup$

I use the command

ClearAll["Global`*"]

so much that I thought it would be nice to have a function that called it. Since by default ClearAll requires an argument, I decided to overload it and have it so when no arguments are passed to ClearAll it clears Global.

Unprotect[ClearAll]

ClearAll[]:=ClearAll["Global`*"];

Protect[ClearAll]

However, mathematica still expects an argument to the function, resulting in this

enter image description here

It still works fine, but is there any way to remove the red arrow? I've tried

SyntaxInformation[ClearAll] := {"ArgumentsPattern" -> {___}} which should allow it to accept 0 arguments, but it still complains about it.

$\endgroup$
  • $\begingroup$ But if you defined ClearAll[] in global context, would not then calling ClearAll[] first time clears your own ClearAll[] that you just defined? Since ClearAll from the system is in the system context Context[ClearAll]. I agree with you, it will be nice to have a system command to clear all, without having to specify the context. I always mixes up the * and the ` order $\endgroup$ – Nasser Jul 16 '14 at 3:26
  • $\begingroup$ In general, you might want to define it as ClearAll[] := ClearAll[$Context <> "*"] $\endgroup$ – rm -rf Jul 16 '14 at 3:36
  • $\begingroup$ @rm-rf @Nasser I actually put my overloaded ClearAll[] into a package which is loaded on startup. As part of the ClearAll[] definition I reload that package after the clear. That information doesn't really have to do with the question though, so I left it out. $\endgroup$ – 1110101001 Jul 16 '14 at 3:40
  • $\begingroup$ @Nasser Since ClearAll is in the System` context any definitions you attach to it will also be in the System` context. $\endgroup$ – Mr.Wizard Jul 16 '14 at 6:49
  • $\begingroup$ @rm-rf "ClearAll::ssym: $Context<>* is not a symbol or a string. >>" -- you probably want ClearAll @@ {$Context <> "*"} $\endgroup$ – Mr.Wizard Jul 16 '14 at 7:02
2
$\begingroup$

In version 10 under Windows I am unable to reproduce the problem you describe:

Unprotect[ClearAll];

ClearAll[] := ClearAll["Global`*"]

SyntaxInformation[ClearAll] = {"ArgumentsPattern" -> {___}};

Protect[ClearAll]

In the Notebook:

enter image description here

Without the definition above:

enter image description here

$\endgroup$
  • $\begingroup$ OP might have issued the SyntaxInformation.. command after doing the Protect like you did? $\endgroup$ – Nasser Jul 16 '14 at 6:59
  • $\begingroup$ @Nasser No, I put it after Unprotect $\endgroup$ – 1110101001 Jul 16 '14 at 6:59
  • $\begingroup$ corrected what I meant above. I typed it wrong. $\endgroup$ – Nasser Jul 16 '14 at 7:00
  • $\begingroup$ It seems that typing all commands directly into the notebook works, but putting it into a .m file loaded at startup causes it to not work... $\endgroup$ – 1110101001 Jul 16 '14 at 7:01
  • $\begingroup$ @user2612743 That actually makes some sense, as something later in the loading process may be resetting SyntaxInformation[ClearAll]. Have you confirmed that it is as you set it after Mathematica is up and running? $\endgroup$ – Mr.Wizard Jul 16 '14 at 7:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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