Everytime, before my code, I always write
SetDirectory[NotebookDirectory[]];
So, how to make this as the default?
Mathematica Stack Exchange is a question and answer site for users of Wolfram Mathematica. It only takes a minute to sign up.
Sign up to join this communityEverytime, before my code, I always write
SetDirectory[NotebookDirectory[]];
So, how to make this as the default?
I'm not sure whether I would suggest to do something like that, but I think the following would basically provide the behavior that you might be after:
SetOptions[$FrontEndSession,
CellProlog :> Replace[
Quiet[NotebookDirectory[]],
{
s_String?DirectoryQ :> SetDirectory[s],
_ :> $UserDocumentsDirectory
}
]
]
it will globally set CellProlog
for the current session. The CellProlog
is evaluated before evaluation of any cell, and the corresponding code will set the directory if NotebookDirectory
returns a valid directory, else it will set directory to $UserDocumentationsDirectory
(of course you could adjust the behaviour to your needs).
Playing with that option might have several drawbacks, be it performance or subtle "unexpected" behavior, so no guarantees that this won't cause more problems than it helps. Another complication is that it might interfer with other functionality which makes use of the CellProlog
option. If you don't see problems, you could make it a permanent setting by using SetOptions
to $FrontEnd
instead of just $FrontEndSession
...
In case you want to switch back to the default, you just need to set CellProlog
back to the default (usually None
) for whatever object you have set it...