Julia REPL
To exit the interactive session, type ^D
– the control key together with the d
key on a blank line – or type exit()
followed by the return or enter key. The REPL greets you with a banner and a julia>
prompt.
REPL 有四种主要的操作模式。第一个也是最常见的是 Julian 提示符。这是默认的操作模式;每个新行最初都以 julia>
开头。就在这里,你可以输入 Julia 表达式。在输入完整表达式后按下 return 或 enter 将执行该表达式,并显示最后一个表达式的结果。
julia> string(1 + 2)
"3"
交互式运行有许多独特的实用功能。除了显示结果外,REPL 还将结果绑定到变量 ans
上。一行的尾随分号可用作禁止显示结果的标志。
julia> string(3 * 4);
julia> ans
"12"
In Julia mode, the REPL supports something called prompt pasting. This activates when pasting text that starts with julia>
into the REPL. In that case, only expressions starting with julia>
are parsed, others are removed. This makes it is possible to paste a chunk of code that has been copied from a REPL session without having to scrub away prompts and outputs. This feature is enabled by default but can be disabled or enabled at will with REPL.enable_promptpaste(::Bool)
. If it is enabled, you can try it out by pasting the code block above this paragraph straight into the REPL. This feature does not work on the standard Windows command prompt due to its limitation at detecting when a paste occurs.
Objects are printed at the REPL using the show
function with a specific . In particular, the :limit
attribute is set to true
. Other attributes can receive in certain show
methods a default value if it’s not already set, like :compact
. It’s possible, as an experimental feature, to specify the attributes used by the REPL via the Base.active_repl.options.iocontext
dictionary (associating values to attributes). For example:
julia> rand(2, 2)
2×2 Array{Float64,2}:
0.8833 0.329197
0.719708 0.59114
julia> show(IOContext(stdout, :compact => false), "text/plain", rand(2, 2))
0.43540323669187075 0.15759787870609387
0.2540832269192739 0.4597637838786053
julia> Base.active_repl.options.iocontext[:compact] = false;
julia> rand(2, 2)
2×2 Array{Float64,2}:
0.2083967319174056 0.13330606013126012
0.6244375177790158 0.9777957560761545
In order to define automatically the values of this dictionary at startup time, one can use the atreplinit
function in the ~/.julia/config/startup.jl
file, for example:
atreplinit() do repl
repl.options.iocontext[:compact] = false
end
Help mode
When the cursor is at the beginning of the line, the prompt can be changed to a help mode by typing ?
. Julia will attempt to print help or documentation for anything entered in help mode:
julia> ? # upon typing ?, the prompt changes (in place) to: help?>
help?> string
search: string String Cstring Cwstring RevString randstring bytestring SubString
string(xs...)
Create a string from any values using the print function.
Macros, types and variables can also be queried:
help?> @time
@time
A macro to execute an expression, printing the time it took to execute, the number of allocations,
and the total number of bytes its execution caused to be allocated, before returning the value of the
expression.
See also @timev, @timed, @elapsed, and @allocated.
help?> Int32
search: Int32 UInt32
Int32 <: Signed
32-bit signed integer type.
Help mode can be exited by pressing backspace at the beginning of the line.
Just as help mode is useful for quick access to documentation, another common task is to use the system shell to execute system commands. Just as ?
entered help mode when at the beginning of the line, a semicolon (;
) will enter the shell mode. And it can be exited by pressing backspace at the beginning of the line.
julia> ; # upon typing ;, the prompt changes (in place) to: shell>
shell> echo hello
hello
Note
For Windows users, Julia’s shell mode does not expose windows shell commands. Hence, this will fail:
julia> ; # upon typing ;, the prompt changes (in place) to: shell>
shell> dir
ERROR: IOError: could not spawn `dir`: no such file or directory (ENOENT)
Stacktrace!
.......
However, you can get access to PowerShell
like this:
julia> ; # upon typing ;, the prompt changes (in place) to: shell>
shell> powershell
Windows PowerShell
Copyright (C) Microsoft Corporation. All rights reserved.
PS C:\Users\elm>
… and to cmd.exe
like that (see the dir
command):
Search modes
Just as ^R
is a reverse search, ^S
is a forward search, with the prompt (i-search)`':
. The two may be used in conjunction with each other to move through the previous or next matching results, respectively.
Key bindings
The Julia REPL makes great use of key bindings. Several control-key bindings were already introduced above (^D
to exit, ^R
and ^S
for searching), but there are many more. In addition to the control-key, there are also meta-key bindings. These vary more by platform, but most terminals default to using alt- or option- held down with a key to send the meta-key (or can be configured to do so), or pressing Esc and then the key.
Julia’s REPL keybindings may be fully customized to a user’s preferences by passing a dictionary to REPL.setup_interface
. The keys of this dictionary may be characters or strings. The key '*'
refers to the default action. Control plus character x
bindings are indicated with "^x"
. Meta plus x
can be written "\\M-x"
or "\ex"
, and Control plus x
can be written "\\C-x"
or "^x"
. The values of the custom keymap must be nothing
(indicating that the input should be ignored) or functions that accept the signature (PromptState, AbstractREPL, Char)
. The REPL.setup_interface
function must be called before the REPL is initialized, by registering the operation with atreplinit
. For example, to bind the up and down arrow keys to move through history without prefix search, one could put the following code in ~/.julia/config/startup.jl
:
import REPL
import REPL.LineEdit
const mykeys = Dict{Any,Any}(
# Up Arrow
"\e[A" => (s,o...)->(LineEdit.edit_move_up(s) || LineEdit.history_prev(s, LineEdit.mode(s).hist)),
# Down Arrow
"\e[B" => (s,o...)->(LineEdit.edit_move_down(s) || LineEdit.history_next(s, LineEdit.mode(s).hist))
)
repl.interface = REPL.setup_interface(repl; extra_repl_keymap = mykeys)
end
atreplinit(customize_keys)
Users should refer to LineEdit.jl
to discover the available actions on key input.
In both the Julian and help modes of the REPL, one can enter the first few characters of a function or type and then press the tab key to get a list all matches:
julia> stri[TAB]
stride strides string strip
julia> Stri[TAB]
StridedArray StridedMatrix StridedVecOrMat StridedVector String
The tab key can also be used to substitute LaTeX math symbols with their Unicode equivalents, and get a list of LaTeX matches as well:
julia> \pi[TAB]
julia> π
π = 3.1415926535897...
julia> e\_1[TAB] = [1,0]
julia> e₁ = [1,0]
2-element Array{Int64,1}:
1
0
julia> e\^1[TAB] = [1 0]
julia> e¹ = [1 0]
1×2 Array{Int64,2}:
1 0
julia> \sqrt[TAB]2 # √ is equivalent to the sqrt function
julia> √2
1.4142135623730951
julia> \hbar[TAB](h) = h / 2\pi[TAB]
julia> ħ(h) = h / 2π
ħ (generic function with 1 method)
julia> \h[TAB]
\hat \hermitconjmatrix \hkswarow \hrectangle
\hatapprox \hexagon \hookleftarrow \hrectangleblack
\hbar \hexagonblack \hookrightarrow \hslash
\heartsuit \hksearow \house \hspace
julia> α="\alpha[TAB]" # LaTeX completion also works in strings
julia> α="α"
A full list of tab-completions can be found in the section of the manual.
Completion of paths works for strings and julia’s shell mode:
julia> path="/[TAB]"
.dockerenv .juliabox/ boot/ etc/ lib/ media/ opt/ root/ sbin/ sys/ usr/
.dockerinit bin/ dev/ home/ lib64/ mnt/ proc/ run/ srv/ tmp/ var/
shell> /[TAB]
.dockerenv .juliabox/ boot/ etc/ lib/ media/ opt/ root/ sbin/ sys/ usr/
.dockerinit bin/ dev/ home/ lib64/ mnt/ proc/ run/ srv/ tmp/ var/
Tab completion can help with investigation of the available methods matching the input arguments:
julia> max([TAB] # All methods are displayed, not shown here due to size of the list
julia> max([1, 2], [TAB] # All methods where `Vector{Int}` matches as first argument
max(x, y) in Base at operators.jl:215
max(a, b, c, xs...) in Base at operators.jl:281
julia> max([1, 2], max(1, 2), [TAB] # All methods matching the arguments.
max(x, y) in Base at operators.jl:215
max(a, b, c, xs...) in Base at operators.jl:281
Keywords are also displayed in the suggested methods after ;
, see below line where limit
and keepempty
are keyword arguments:
julia> split("1 1 1", [TAB]
split(str::AbstractString; limit, keepempty) in Base at strings/util.jl:302
split(str::T, splitter; limit, keepempty) where T<:AbstractString in Base at strings/util.jl:277
The completion of the methods uses type inference and can therefore see if the arguments match even if the arguments are output from functions. The function needs to be type stable for the completion to be able to remove non-matching methods.
Tab completion can also help completing fields:
julia> import UUIDs
julia> UUIDs.uuid[TAB]
uuid1 uuid4 uuid_version
Fields for output from functions can also be completed:
julia> split("","")[1].[TAB]
lastindex offset string
The completion of fields for output from functions uses type inference, and it can only suggest fields if the function is type stable.
julia> foo = Dict("qwer1"=>1, "qwer2"=>2, "asdf"=>3)
Dict{String,Int64} with 3 entries:
"qwer2" => 2
"asdf" => 3
"qwer1" => 1
julia> foo["q[TAB]
"qwer1" "qwer2"
julia> foo["qwer
Customizing Colors
The colors used by Julia and the REPL can be customized, as well. To change the color of the Julia prompt you can add something like the following to your ~/.julia/config/startup.jl
file, which is to be placed inside your home directory:
The available color keys can be seen by typing Base.text_colors
in the help mode of the REPL. In addition, the integers 0 to 255 can be used as color keys for terminals with 256 color support.
You can also change the colors for the help and shell prompts and input and answer text by setting the appropriate field of repl
in the customize_colors
function above (respectively, , shell_color
, input_color
, and answer_color
). For the latter two, be sure that the envcolors
field is also set to false.
It is also possible to apply boldface formatting by using Base.text_colors[:bold]
as a color. For instance, to print answers in boldface font, one can use the following as a ~/.julia/config/startup.jl
:
function customize_colors(repl)
repl.envcolors = false
repl.answer_color = Base.text_colors[:bold]
end
atreplinit(customize_colors)
You can also customize the color used to render warning and informational messages by setting the appropriate environment variables. For instance, to render error, warning, and informational messages respectively in magenta, yellow, and cyan you can add the following to your ~/.julia/config/startup.jl
file:
ENV["JULIA_ERROR_COLOR"] = :magenta
ENV["JULIA_WARN_COLOR"] = :yellow
ENV["JULIA_INFO_COLOR"] = :cyan
TerminalMenus is a submodule of the Julia REPL and enables small, low-profile interactive menus in the terminal.
Examples
import REPL
using REPL.TerminalMenus
options = ["apple", "orange", "grape", "strawberry",
"blueberry", "peach", "lemon", "lime"]
RadioMenu
The RadioMenu allows the user to select one option from the list. The request
function displays the interactive menu and returns the index of the selected choice. If a user presses ‘q’ or ctrl-c
, request
will return a -1
.
# `pagesize` is the number of items to be displayed at a time.
# than the `pagesize`
menu = RadioMenu(options, pagesize=4)
# `request` displays the menu and returns the index after the
# user has selected a choice
choice = request("Choose your favorite fruit:", menu)
if choice != -1
println("Your favorite fruit is ", options[choice], "!")
else
println("Menu canceled.")
end
Output:
Choose your favorite fruit:
^ grape
strawberry
> blueberry
v peach
Your favorite fruit is blueberry!
MultiSelectMenu
The MultiSelectMenu allows users to select many choices from a list.
# here we use the default `pagesize` 10
menu = MultiSelectMenu(options)
# `request` returns a `Set` of selected indices
# if the menu us canceled (ctrl-c or q), return an empty set
choices = request("Select the fruits you like:", menu)
if length(choices) > 0
println("You like the following fruits:")
for i in choices
println(" - ", options[i])
end
else
println("Menu canceled.")
end
Output:
Select the fruits you like:
[press: d=done, a=all, n=none]
[ ] apple
> [X] orange
[X] grape
[ ] strawberry
[ ] blueberry
[X] peach
[ ] lemon
[ ] lime
You like the following fruits:
- orange
- grape
- peach
All interface customization is done through the keyword only TerminalMenus.config()
function.
Arguments
charset::Symbol=:na
: ui characters to use (:ascii
or:unicode
); overridden by other argumentscursor::Char='>'|'→'
: character to use for cursorup_arrow::Char='^'|'↑'
: character to use for up arrowdown_arrow::Char='v'|'↓'
: character to use for down arrowchecked::String="[X]"|"✓"
: string to use for checkedunchecked::String="[ ]"|"⬚")
: string to use for uncheckedscroll::Symbol=:na
: If:wrap
then wrap the cursor around top and bottom, if :nowrap
do not wrap cursorsupress_output::Bool=false
: For testing. If true, menu will not be printed to console.ctrl_c_interrupt::Bool=true
: Iffalse
, return empty on ^C, iftrue
throw InterruptException() on ^C
Examples
julia> menu = MultiSelectMenu(options, pagesize=5);
julia> request(menu) # ASCII is used by default
[press: d=done, a=all, n=none]
[ ] apple
[X] orange
[ ] grape
> [X] strawberry
v [ ] blueberry
Set([4, 2])
julia> TerminalMenus.config(charset=:unicode)
julia> request(menu)
[press: d=done, a=all, n=none]
⬚ apple
✓ orange
⬚ grape
→ ✓ strawberry
↓ ⬚ blueberry
Set([4, 2])
julia> TerminalMenus.config(checked="YEP!", unchecked="NOPE", cursor='⧐')
julia> request(menu)
[press: d=done, a=all, n=none]
NOPE apple
YEP! orange
NOPE grape
⧐ YEP! strawberry
↓ NOPE blueberry
Set([4, 2])
References
— Function
atreplinit(f)
Register a one-argument function to be called before the REPL interface is initialized in interactive sessions; this is useful to customize the interface. The argument of f
is the REPL object. This function should be called from within the initialization file.