Change julia promt to include evalutation numbers
Asked Answered
B

2

5

When debugging or running julia code in REPL, I usually see error messages showing ... at ./REPL[161]:12 [inlined].... The number 161 means the 161-th evaluation in REPL, I guess. So my question is could we show this number in julia's prompt, i.e. julia [161]> instead of julia>?

Bursiform answered 2/1, 2018 at 4:29 Comment(1)
You could investigate OhMyREPL, this Discourse post, and TerminalExtensions. But the answer is basically no (at least not easily).Amiss
K
8

One of the advantages of Julia is its ultra flexibility. This is very easy in Julia 0.7 (nightly version).

julia> repl = Base.active_repl.interface.modes[1]
"Prompt(\"julia> \",...)"

julia> repl.prompt = () -> "julia[$(length(repl.hist.history) - repl.hist.start_idx + 1)] >"
#1 (generic function with 1 method)

julia[3] >

julia[3] >2
2

julia[4] >f = () -> error("e")
#3 (generic function with 1 method)

julia[5] >f()
ERROR: e
Stacktrace:
 [1] error at .\error.jl:33 [inlined]
 [2] (::getfield(, Symbol("##3#4")))() at .\REPL[4]:1
 [3] top-level scope

You just need to put the first 2 lines onto your ~/.juliarc and enjoy~

Since there are several changes in the REPL after julia 0.7, these codes do not work in old versions.

EDIT: Well, actually there need a little bit more efforts to make it work in .juliarc.jl. Try this code:

atreplinit() do repl
    repl.interface = Base.REPL.setup_interface(repl)
    repl = Base.active_repl.interface.modes[1]
    repl.prompt = () -> "julia[$(length(repl.hist.history) - repl.hist.start_idx + 1)] >"
end
Kelcy answered 2/1, 2018 at 12:31 Comment(5)
I would rename repl (where it represents Prompt type) to base_mode to not mix one name for 2 different meanings/types. (not only for didactic reasons)Damiondamita
Cool! Got any links to relevant documentation?Amiss
That's great, thanks! julia 0.7 release will come soon anyway.Bursiform
@Damiondamita Yes, thanks. I didn't consider the name when I wrote the code before dropping them to .juliarc.jlNightfall
@Amiss I learnt the REPL stuff in this code and how to show line number hereNightfall
D
2

just to update to julia >v1.9, a numbered prompt was added

add the following to your startup.jl

atreplinit() do repl
    try
        @eval using REPL
    catch e
        @warn "error while imporing REPL" e
    end
    if !isdefined(repl, :interface)
        repl.interface = REPL.setup_interface(repl)
    end
    REPL.numbered_prompt!(repl)
end
Declarant answered 9/11, 2023 at 10:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.