REPL copy/paste resulting in UndefVarError 'julia' not defined

Copying and pasting julia code from documentation or help mode resulting in UndefVarError ‘julia’ not defined.

Specifically I copied this line of code from help mode for the filter function:

 julia> map(filter(iseven), [1:3, 2:4, 3:5])

I then pasted it into my REPL as follows:

julia>  julia> map(filter(iseven), [1:3, 2:4, 3:5])
ERROR: UndefVarError: `julia` not defined
Stacktrace:
 [1] top-level scope @ REPL[5]:1

julia>

I copied this line of code from the official julia documentation from this https://docs.julialang.org/en/v1/stdlib/REPL/ and got the same behavior

julia> julia> string(1 + 2)
ERROR: UndefVarError: `julia` not defined
Stacktrace:
 [1] top-level scope @ REPL[7]:1

I seem to remember doing things like this in the past and the REPL would automatically remove julia> from the text and paste the rest allowing the pasted code to run as expected. Did I imagine this? If not, has anyone else experience similar behavior with this version of julia or is this just an an issue with my install?

Would appreciate any tips on how to best navigate this behavior.

my version info is as follows:

julia> versioninfo()
Julia Version 1.9.3
Commit bed2cd540a (2023-08-24 14:43 UTC)
Build Info:
  Official https://julialang.org/ release
Platform Info:
  OS: Windows (x86_64-w64-mingw32)
  CPU: 6 × Intel(R) Core(TM) i5-9400F CPU @ 2.90GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-14.0.6 (ORCJIT, skylake)
  Threads: 1 on 6 virtual cores

julia>
2 Likes

From the docs on the REPL’s julia> mode:

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

Seems relevant, were you working on a different OS before?

1 Like

Thanks for the suggestion @Benny. That didn’t seem to do the trick though, see below.

julia> import REPL

julia> REPL.enable_promptpaste(true)
true

julia> julia> string(1 + 2)
ERROR: UndefVarError: `julia` not defined
Stacktrace:
 [1] top-level scope @ REPL[3]:1

julia>

I also wiped my startup file to see if something in there was causing the problem but still got the same behavior. My start-up file is as follows:

try
    @eval using Revise
catch e
    @warn "Error initializing Revise" exception=(e, catch_backtrace())
end

try
    @eval using RelevanceStacktrace
catch e
    @warn "Error initializing RelevanceStacktrace" exception=(e, catch_backtrace())
end

atreplinit() do repl
    @eval begin
        import JuliaSyntax
        JuliaSyntax.enable_in_core!(true)
    end
end

try
    @eval using JuliaFormatter
catch e
    @warn "Error initializing JuliaFormatter" exception=(e, catch_backtrace())
end

atreplinit() do repl
    try
        @eval begin
		import OhMyREPL
		OhMyREPL.enable_autocomplete_brackets(true)
	end
    catch e
        @warn @warn "Error enabling autocomplete brackets" exception=(e, catch_backtrace())
    end
end

using TerminalPager
TerminalPager.set_preference!("always_use_alternate_screen_buffer_in_repl_mode", true)
TerminalPager.set_preference!("active_search_decoration", "45")

if Base.isinteractive() &&
   (local REPL = get(Base.loaded_modules, Base.PkgId(Base.UUID("3fa0cd96-eef1-5676-8a61-b3b8758bbffb"), "REPL"), nothing); REPL !== nothing)

    # Exit Julia with :q, restart with :r
    pushfirst!(REPL.repl_ast_transforms, function(ast::Union{Expr,Nothing})
        function toplevel_quotenode(ast, s)
            return (Meta.isexpr(ast, :toplevel, 2) && ast.args[2] === QuoteNode(s)) ||
                   (Meta.isexpr(ast, :toplevel) && any(x -> toplevel_quotenode(x, s), ast.args))
        end
        if toplevel_quotenode(ast, :q)
            exit()
        elseif toplevel_quotenode(ast, :r)
            argv = Base.julia_cmd().exec
            opts = Base.JLOptions()
            if opts.project != C_NULL
                push!(argv, "--project=$(unsafe_string(opts.project))")
            end
            if opts.nthreads != 0
                push!(argv, "--threads=$(opts.nthreads)")
            end
            run(Cmd(argv))
	    exit()
        end
        return ast
    end)

    # Automatically load tooling on demand:
    # - BenchmarkTools.jl when encountering @btime or @benchmark
    # - Cthulhu.jl when encountering @descend(_code_(typed|warntype))
    # - Debugger.jl when encountering @enter or @run
    # - Profile.jl when encountering @profile
    # - ProfileView.jl when encountering @profview
    local tooling_dict = Dict{Symbol,Vector{Symbol}}(
        :BenchmarkTools => Symbol.(["@btime", "@benchmark"]),
        :Cthulhu        => Symbol.(["@descend", "@descend_code_typed", "@descend_code_warntype"]),
        :Debugger       => Symbol.(["@enter", "@run"]),
        :Profile        => Symbol.(["@profile"]),
        :ProfileView    => Symbol.(["@profview"]),
    )
    pushfirst!(REPL.repl_ast_transforms, function(ast::Union{Expr,Nothing})
        function contains_macro(ast, m)
            return ast isa Expr && (
                (Meta.isexpr(ast, :macrocall) && ast.args[1] === m) ||
                any(x -> contains_macro(x, m), ast.args)
            )
        end
        for (mod, macros) in tooling_dict
            if any(contains_macro(ast, s) for s in macros) && !isdefined(Main, mod)
                @info "Loading $mod ..."
                try
                    Core.eval(Main, :(using $mod))
                catch err
                    @info "Failed to automatically load $mod" exception=err
                end
            end
        end
        return ast
    end)

end

Also my machine hasn’t changed in recent years. I have always used Windows and I’ve been on this particular PC for a few years. I typically run julia in gitbash or just julia REPL itself.