Useful options for interaction with Julia
Julia has powerful functionalities built into its console that make your daily workflow more efficient. In this recipe, we will investigate some useful options in an interactive session.
Getting ready
Create anexample.jl
file containing this:
println("An example was run!")
We will run this script in this recipe.
Note
In the GitHub repository for this recipe you will find the commands.txt
file that contains the presented sequence of shell and Julia commands and the example.jl
file described above.
Now open your favorite terminal to execute the commands.
How to do it...
We will learn how to work interactively with Julia by going through the following steps:
- Start the Julia command line.
- Execute two commands in the Julia command line:
julia> x = 10 # just a test command 10 julia> @edit sin(1.0)
After running these commands, an editor with the location of a code section containing the sin
function opens. We explained earlier how to choose the editor that Julia uses in the How to customize Julia on startup recipe.
- Close the editor to get back to Julia.
- Now press Ctrl + L. You will notice that the screen was redrawn and the output from previous commands was cleared.
Now, let us check if example.jl
is in our current working directory.
- Press ; key and the prompt in Julia should change to this:
shell>
- Type
ls
if you are on Linux ordir
in Windows, to execute the shell command. You should get a list of files in your current working directory and after this command, Julia comes back to a standard prompt. When you are sure you have theexample.jl
file in your working directory, we can continue.
- Start by typing
inc
in the Julia console:
julia> inc
- Press Tab. Julia will autocomplete it to
include
, a built-in function in Julia:
julia> include
- Next, continue by entering the text
("exa
in the Julia console:
julia> include("exa
- Press Tab again to get the following:
julia> include("example.jl"
- Finally, type
)
and hit Enter. Runninginclude
will execute the commands given in theexample.jl
file. At this point, you would probably like to understand what function theinclude
command performs.
- Press ? in Julia REPL to switch to
help
mode. The prompt will change to the following:
help?>
- Start writing the command you want to check by pressing
in
:
help?> in
- Next, press Tab twice to get the following:
help?> in in include_string indexin indmax init_worker interrupt inv invoke include ind2chr indexpids indmin insert! intersect invdigamma invperm include_dependency ind2sub indices info instances intersect! invmod help?> in
This time we see that there are multiple commands matching the in
pattern and Julia lists them all (this is the reason that the Tab key had to be pressed twice).
- Press
c
and press Tab—now there is only one feasible completion that is filled. - Press Enter to get the following:
help?> include
search: include include_string include_dependency
include(path::AbstractString)
Evaluate the contents of the input source file in the global scope of the
containing module. Every module (except those defined with baremodule) has its
own 1-argument definition of include, which evaluates the file in that module.
Returns the result of the last evaluated expression of the input file. During
including, a task-local include path is set to the directory containing the file.
Nested calls to include will search relative to that path. This function is
typically used to load source interactively, or to combine files in packages that
are broken into multiple source files.
Use Base.include to evaluate a file into another module.
And we understand exactly what include
does. Now, what if we wanted to run the x = 10
command again (this is mostly useful for longer and complex commands in practice)?
- Press Ctrl + R to switch Julia into reverse search mode and type
x =
to get the following:
(reverse-i-search)`x =': x = 10
- Press Enter to have the command you found inserted into the Julia prompt:
julia> x = 10
- Press Enter to execute the command. Alternatively, we could use arrow up/down or page up/down to traverse the command history.
- Finally, to terminate Julia, you can either press Ctrl + D or run the
exit()
function.
How it works...
Julia REPL offers you several modes, of which the most commonly used are these:
- Julian: For the execution of Julia code (this is the default).
- Help: Started by pressing the ? key. As you proceed, you will find instructions on how to use this mode.
- Shell: Started by pressing the ; key
.
In this mode, you can quickly execute a shell command without leaving Julia. - Package manager: Started by pressing the ] key. In this mode, you can manage packages installed in your system.
- Backward search mode, which you can enter using Ctrl + R.
You can find more details about options for interacting with Julia in all those modes at https://docs.julialang.org/en/v1.0/stdlib/REPL/.
As you can observe, Julia is smart enough to perform tab completion in a context-sensitive manner—it understands if you are entering a command or a filename. Command history search is also very useful in interactive work.
In the How to customize Julia on startup recipe, we explained how to set up the editor. In this recipe, we saw how you can use the @edit
macro to open the location of the definition of the sin
function in your chosen editor. Julia recognizes the following editors: Vim, Emacs, gedit, textmate, mate, kate, Sublime Text, atom, Notepad++, and Visual Studio Code. Importantly, @edit
recognizes the types of arguments you pass to a function and will show an appropriate method if your chosen editor supports line search on startup (otherwise, an appropriate file will be opened and the line number of the function at hand will be printed in the Julia command line).
There's more...
Apart from the @edit
macro, you can use the @less
macro or the edit
and less
functions to see the source code of the function you wish to use (please consult the Julia help guide to understand the detailed differences between them).
If we only want to know the location of a method definition without displaying it, we can use the @which
macro:
julia> @which sin(1.0)
sin(x::T) where T<:Union{Float32, Float64} in Base.Math at special/trig.jl:30
See also
The How to customize Julia on startup recipe explains how to use the startup.jl
file and how to choose the default Julia editor.