A nice feature of Julia is that supports startup file that contains the code that is executed every time you start a new Julia session.
This functionality is described in the Julia Manual. In this post I want to discuss this functionality from the user’s perspective.
The basics of startup file
The startup file is typically located at
(I am using Linux defaults here). However, this default location can be modified.
You can change it by manipulating environment variables. These more advanced
configuration options are described here in the Julia Manual.
startup.jl file contains the commands that are executed when Julia is run.
What are typical entries of the startup file?
Usually people load utility packages that they routinely need when working with Julia (to avoid having to manually load them each time). Some popular examples are:
- Revise.jl: allowing to modify source code without having to restarting Julia;
- OhMyREPL.jl: advanced highlighting support in Julia terminal;
- JET.jl: code analyzer for Julia;
- BenchmarkTools.jl: performance tracking of Julia code.
Another use programmatic setting of preferences, for example:
- setting default code editor via
- automatic activation of project environment; I discussed it some time ago in this post.
Selective execution of code in startup file
Julia can be started in two modes:
- interactive (called REPL);
- script execution.
Some of the features are useful only in REPL mode. For example, loading OhMyREPL.jl probably does not have much value added when executing a script.
Julia allows you to add code that should run only when it is activated in REPL mode
by registering a function that is run only in this mode. This registration is achieved
atreplinit function that should be defined in your
It is done in the following way (the code inside the function is just an example):
atreplinit() do repl
println("This is printed only in REPL, but not when executing a script")
Disabling loading of a startup file
In some cases, especially if you run some third party Julia code, you might want
to disable loading the
startup.jl file. This can be achieved by
--startup-file=no command line argument.
Why could you want to do it? In this way you make sure that the code you have in
startup.jl does not conflict with the code you want to run (this situation
is rare, but is possible).
Some Julia users prefer not to define
startup.jl at all and always be explicit
about what is loaded when Julia is started. This scenario is probably most common
when someone mostly runs Julia scripts as it ensures a clean environment and fastest
However, in many users, especially if they work interactively a lot, like to initialize their Julia session each time it is started with some standard code. For example, if you are a data scientist you might want to always load for example CSV.jl, DataFrames.jl, Statistics.jl, and StatsBase.jl when you are working in REPL mode to avoid having to load these packages manually every time you start a new session.
When working with
startup.jl files there are two things worth remembering:
- You can selectively decide which code is loaded only if you work in REPL mode
- Even if you have
startup.jlfile you can fully disable loading it by passing the
--startup-file=nocommand line option.