juliaprompt

Change julia promt to include evalutation numbers


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>?


Solution

  • 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