Julia Win10 - REPL and OhMyREPL - neat trick for nice copy/past - works in VsCode, WT, etc

First all credit goes to @ MarcMush for the idea in this thread (Apr 25).

I only quasi-repost because there’s an added step for making it works with VsCode and because I stumbled on it after having be bogged down by the issue for quite some time… maybe useful to others.

What this solves: slow past of multiline text into std REPL, very slow past and broken indentation into OhMyREPL.

  1. Tweak your startup.jl to include the snippet below. This defines a keybinding for pasting using Julia clipboard() function. In VsCode it is bound to Ctrl+V and replace the standard past (emulate so called bracketed past)
  2. For VsCode integrated “Julia REPL”, add the snippet below to your keybindings.json - command Preferences: Open Keyboard Shortcut (JSON)

Result: instant cut/past of large code text, with correct indentation and colors. By Ctrl+V in VsCode and by Ctrl+N in other terminals.

By the way… if you like the command line… I strongly recommend that you install Windows Terminal. Dev by Microsoft, on GitHub, open source, installed by the store. Shortcut “wt”. Provide a full *nix grad terminal with true tty backing, tabs, panes, etc… You can add profiles to have bash (from e.g. Git-for-Windows) and Julia.

Since Win10 the awful conhost (the win95 non-equivalent of unix tty) has been replaced by contty. So now Windows actually has true full featured tty. With WT you get an xterm over this tty. The new tty is used by powershell and new cmd… but currently not by Julia (julia.exe default install still use conhost, so it’s awful, running it in WT should be the default).

.julia/config/startup.jl

if isinteractive()
    # ......

    using OhMyREPL
    # your usual config of here. No need to change it...
    colorscheme!("CustomColors")
    enable_autocomplete_brackets(false)
    enable_highlight_markdown(true)
    # fuzzy history search. Does not work in git-bash mintty, but work well in VSCode term & WT
    enable_fzf(get(ENV,"TERM_PROGRAM","") != "mintty")

    # Here is the nice idea of MarcMush
    using REPL
    atreplinit() do repl
        if repl isa LineEditREPL
            bind = !haskey(ENV,"SHELL") && get(ENV,"TERM_PROGRAM","") == "vscode" ? "V" : "N"
            printstyled("Bind Ctrl+$bind: Past Code Block\n"; color=:blue)
            Base.active_repl.interface = REPL.setup_interface(Base.active_repl; extra_repl_keymap = Dict{Any,Any}(
                "^"*bind => (s, data, c) -> 
                    begin
                        REPL.LineEdit.edit_insert(REPL.LineEdit.buffer(s), REPL.InteractiveUtils.clipboard())
                        OhMyREPL.Prompt.rewrite_with_ANSI(s)
                    end
            ))
        end
    end

   # ......
end

keybindings.json

   // in  ...\AppData\Roaming\Code\User\keybindings.json
   // remove Ctrl+V keybind for the all terminals
   {
      "key": "ctrl+v",
      "command": "-workbench.action.terminal.paste",
      "when": "terminalFocus && terminalProcessSupported"
   },
   // re-add Ctrl+V keybind for the all terminals BUT for the Julia REPL session
   {
      "key": "ctrl+v",
      "command": "workbench.action.terminal.paste",
      "when": "terminalFocus && terminalProcessSupported && !isJuliaREPL"
   },
2 Likes

Hi, is this really working?
According to

the isinteractive() is false in the startup.jl, so this never executes. I have tried it.

Yes its working :wink:
You have to tell julia that you intend to use it in interactive mode, that is you have to pass the -i option.
This is what isinteractive() do: it tests if -i was passed to julia.
Note that vscode will pass -i to the julia process it spawns when opening a REPL.