22
$\begingroup$

What is the right folder to put a file init.m which will be run after all other init.m files applied by the various components of Mathematica have done their job?

I would like to use it to do something like

 AppendTo[$Path, *my favourite place*];
 Unprotect[Needs];
 Needs[x_] := Needs[x, ContextToFileName[x]]; 
 Protect[Needs]; 
 Needs["CleanSlate`"];
 SetOptions[FileNames, IgnoreCase->True];
 ClearAttributes[If, HoldAll]; SetAttributes[If, HoldRest];
 ClearAttributes[Switch,HoldAll]; SetAttributes[Switch, HoldRest];
 Unprotect[RGBColor]; 
 RGBColor[{x_, y_, z_}] := RGBColor[x, y, z]; 
 Protect[RGBColor];
 Unprotect[MantissaExponent]; 
 MantissaExponent[i_?IntegerQ] := MantissaExponent[1.*i]
 Protect[MantissaExponent];

... and other things which make my work go smoother.

Previously (in Version 4.0)

C:\Program Files\Wolfram Research\Mathematica\4.0\AddOns\Autoload\init.m

or

C:\Program Files\Wolfram Research\Mathematica\4.0\SystemFiles\FrontEnd\init.m

were the right places for this type of operations.

$\endgroup$
2

2 Answers 2

22
$\begingroup$

There are two typical locations where a user can add an init.m file to be loaded at run-time: $BaseDirectory and $UserBaseDirectory. $BaseDirectory is useful for installation wide customizations that apply to all users while $UserBaseDirectory is used for a specific users customizations. So, anything in $UserBaseDirectory is loaded after the files found in $BaseDirectory so that the user's preferences are heeded. For most users, this distinction is likely not to matter as they are the only user of their computer. However, I would recommend using $UserBaseDirectory as it is less messy, and it provides you with several opportunities for customization. From this tutorial on the system file layout, there are two locations where a global init.m will be picked up on load:

  • FileNameJoin[{$UserBaseDirectory,"Kernel"}]
  • FileNameJoin[{$UserBaseDirectory,"FrontEnd"}]

where an init.m file will be loaded when the respective component is loaded (kernel or front-end). The subdirectories in $UserBaseDirectory is also where your custom style-sheets would go, but that is off topic for the question.

$\endgroup$
3
  • 4
    $\begingroup$ Files in $UserBaseDirectory will (should) also remain when you uninstall Mathematica or upgrade versions. They are also simpler to back-up. $\endgroup$
    – Mr.Wizard
    Oct 21, 2014 at 18:28
  • $\begingroup$ But FileNameJoin[{$UserBaseDirectory,"FrontEnd"}] is the one, which is rewritten on every start of Mathematica. This is certainly not the right place to store any customization! $\endgroup$ Oct 22, 2014 at 16:27
  • $\begingroup$ @AdalbertHanßen yes and no. My take on it is that the FE reads in init.m, and then spits out a modified version based upon what has changed during the session. In fact, if it finds stuff in it doesn’t recognize, it will assume it is corrupt and replace it with the default version which can be a bit disconcerting upon opening. However, the settings within the SetOptions[$FrontEnd,...] can be modified, and they are respected. So, it looks like you’re limited to the FrontEnd Options. $\endgroup$
    – rcollyer
    Oct 22, 2014 at 16:54
1
$\begingroup$

The place given by $UserBaseDirectory or by $UserDirectory did not work for me in Version 10.4.0 for Microsoft Windows (64-bit) (February 26, 2016).

But adding a file init.m to C:\ProgramData\Mathematica\Kernel worked for me.

To test it, I defined a variable and assigned it a value and checked it in the subsequent Mathematica session: It was defined and had the right value.

$\endgroup$

Your Answer

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

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