How to use Julia in the REPL
You might be familiar with using Julia in a Jupyter notebook. However, there are situations where you want to use Julia in the REPL. Most commonly when you want to quickly test small snippets of code. This document explains how to do so.
1 Start Julia
To start Julia in the REPL, type julia
in the terminal.
Alternatively you can start the REPL in a specific environment by typing
julia --project
while in the environment folder. The REPL
will start, and you can start typing Julia code. To exit the REPL, type
exit()
.
2 Julia REPL modes
The Julia REPL has multiple modes. When starting the Julia REPL, you
are in the Julia mode. Here you can type and execute Julia code similar
to the way it works in Jupyter notebook. Type the code you want to
execute, then press enter
to execute the code. The result
will be printed right below it. Variables that you declare this way will
be in global scope and as such be remembered by following code snippets
in the same session. To clear all previously defined variables you have
to restart the session. Apart from that, the most important modes are
the package mode and the help mode.
2.1 Package mode
Loading package can be done in the package mode. To enter the package
mode, type ]
at the beginning of a line. In this mode, you
can for example add add
, remove rm
and update
up
packages. You can also activate a specific environment
by typing activate
, compile the current environment with
precompile
and instantiate all the packages in the
Project.toml file of your currently active environment with
instantiate
. Like all modes in the Julia REPL, you can exit
the package mode by typing backspace
at the beginning of a
line.
2.2 Help mode
If you are not sure what a specific Julia function does, you can
enter the help mode by typing ?
at the beginning of a line.
In this mode, you can type the name of the function you would like to
know more about. The REPL will show you the documentation of the
function. Like all modes in the Julia REPL, you can exit the package
mode by typing backspace
at the beginning of a line.
3 Summary
start Julia in the terminal by typing
julia
exit the REPL by typing
exit()
you can use the REPL similar to a Jupyter notebook
enter the package mode by typing
]
and exit it by typingbackspace
enter the help mode by typing
?
and exit it by typingbackspace
in the package mode you can add, remove and update packages, activate environments, compile the current environment and instantiate all the packages in the Project.toml file of your current environment
in the help mode, you can get the documentation of a specific function