1
$\begingroup$

The autocompletion is very convient in Mathematica,but it will make one annoying sometimes when you have a ton variables.I give a scene I encounter.I usually make many custom functions in my init.m,for example

System`testFunction[variablea_]:=Module[{variableb=variablea},variableb+1]

http://o8aucf9ny.bkt.clouddn.com/2016-06-25-01-06-52.png

But when I launch my Mathematica as soon as there are many variables prompt to auto-autocomplete.like this

So my question is how to make the variables in init.m not impact the autocompletion

$\endgroup$

1 Answer 1

6
$\begingroup$

Don't put symbols you don't want to show up in auto-completion into the Global` context. Use the same mechanism you would for a package.

myfun; (* create in Global` *)
Begin["MyPrivateInitContext`"]
myfun[x_] := Module[{y=x^2}, y^2]
End[]

I noticed you are putting your function into the System` context. This seems like a bad idea. The System` context is for builtins.


What I do personally is that I create a complete package and load it with Needs in init.m. This way the package lives in its own separate context, i.e. neither in System or Global.

If the package is very simple, it can also be inlines in init.m: just put the same code that would go in the package file into init.m

If these definitions need to be accessible from subkernels (when parallelizing), then use the Autoload directory instead of init.m.

$\endgroup$
5
  • $\begingroup$ Upvote.The System` can prevent my custom function losing its definition from Clear["`*"] $\endgroup$
    – yode
    Commented Jun 24, 2016 at 17:27
  • 1
    $\begingroup$ @yode So can any other context. Clear["`*"] only clears Global` $\endgroup$
    – Szabolcs
    Commented Jun 24, 2016 at 17:28
  • $\begingroup$ You save me again. $\endgroup$
    – yode
    Commented Jun 24, 2016 at 17:42
  • $\begingroup$ @Szabolcs But lower case functions are sometimes useful in the System` context: you can use them in other packages without trouble. Withy your Global`myfun you will have to use the longname explicitly in any package. So why not Begin["MyPrivateInitContext`"]; System`testFunction[variablea_] := Module[{variableb = variablea}, variableb + 1]; End[]; ? $\endgroup$ Commented Jun 24, 2016 at 20:18
  • 1
    $\begingroup$ @Rolf Actually I am not advocating using Global. I think the right way is to use a custom context. If we want to use this in packages, we can always add it to the $ContextPath with one line after BeginPackage. Even better, just put the definitions in a proper package and load the package in init.m. Inside another package, load it with Needs or BeginPackage. What I do personally is that I put the definitions in a package and put the package in Autoload. This way I can also use it from subkernels when parallelizing. $\endgroup$
    – Szabolcs
    Commented Jun 24, 2016 at 20:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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