If you have code that you want to run every time you start Julia, add it to your startup file that is located at ~/.julia/config/startup.jl
. Note that you might have to first create the config folder.
A common use-case for the startup.jl
file is to load packages that are crucial for your workflow. However, be cautious about adding too many packages, as this can significantly increase the loading time of your REPL and potentially pollute the global namespace. It's best to include only essential packages that you use frequently. There are however two packages I personally consider essential additions: Revise.jl and OhMyRepl.jl.
Warning: First install packages
Before adding a package to your startup.jl
, first add it to your global (@v1.10)
environment!
Julia makes use of something called stacked environments:
Stacked environments allow for adding tools to the primary environment. You can push an environment of development tools onto the end of the stack to make them available from the REPL and scripts, but not from inside packages.
Before the environment of a package or project is activated, Julia first loads your global (@v1.10)
environment and evaluates the code in your startup.jl
.
Revise.jl will keep track of changes in loaded files and reload modified Julia code without having to start a new REPL session.
To load Revise automatically, add the following code to your startup.jl
:
# Add to ~/.julia/config/startup.jl
try
using Revise
catch e
@warn "Error initializing Revise in startup.jl" exception=(e, catch_backtrace())
end
It is enough to add using Revise
, but the try-catch
statement will return a helpful error message in case something goes wrong.
VSCode
Revise is loaded as part of the Julia VSCode plugin. If you are using VSCode, adding Revise to your startup.jl
isn't necessary.
OhMyRepl adds many features to your REPL, among other things:
syntax highlighting
rainbow bracket highlighting
fuzzy history search
stripping prompts when pasting code
# Add to ~/.julia/config/startup.jl
atreplinit() do repl
try
@eval using OhMyREPL
catch e
@warn "Error initializing OhMyRepl in startup.jl" exception=(e, catch_backtrace())
end
end
You can also add the following to your startup.jl
:
TestEnv.jl: Quickly activate your test environment using TestEnv.activate()
BenchmarkTools.jl: Measure the performance of your code using the @benchmark
macro.
BasicAutoloads.jl: "whenever I type this in the REPL, run that for me".
environment variables via the ENV
dictionary
custom functions that you commonly use