Nice weird feature on pasting julia> code

on macos and linux, copy

julia> x=2

julia> x + 1

into the copy-paste buffer.

obviously, this should not be pasted into a running REPL, because each line starts with julia> .

except, the REPL is smart enough to recognize this somehow. in fact, on paste, the paste-buffer julia> does not show up on the screen.

typing the code into the REPL does not work. the 'julia> ’ becomes visible, and then it does not work.

so, how does this feature work? is this special paste functionality in the julia REPL?

3 Likes

How did I not notice that this works until now? This wants to be part of the discourse FAQ/PSA, amazing.

1 Like

The Julia REPL · The Julia Language last paragraph.

10 Likes

this is a great feature addition. love it. alas, why did it now work when I typed it:

julia> julia> x=2
> (generic function with 1 method)

julia>  julia> x=2
> (generic function with 1 method)

similarly. it does not work with julia source code:

$ cat test.jl
julia> x=2

julia> x=x+1

julia> println(x)

^D

$ julia test.jl
ERROR: LoadError: UndefVarError: x not defined
Stacktrace:

It is only active when pasting (and the terminal supports detecting paste, bracketed paste mode).

2 Likes

great. mille grazie. I did not know that terminal-paste-detection-passthrough existed.

This is a good reminder to take a look at the doc. I have never really spent the time in going through the key bindings before.

I found some surprises for myself.

  1. Setting a mark and coming back to it (C-space and C-x C-x)
  2. Moving forward/backward by word (M-f and M-b)
  3. Transpose characters! (C-t, and I had fun C-t all the way to move a char to the end of line)
  4. etc.

But some functions don’t work (I’m using Mac):

  1. M-left/right arrow does not indent/deindent. Instead, it moves cursor left/right a word similar to M-f and M-b
  2. C-/ does nothing but C-_ works properly.

We should also add ] for entering the package manager mode in the doc.

Doesn’t Base.REPL.enable_promptpaste(::Bool) needs to be replaced by REPL.enable_promptpaste(::Bool) in julia 1.0 doc?

Yes. But the whole option should probably just be removed.

As in just enable it unconditionally?

Yeah, I don’t think anyone has ever disabled it. I think the only reason to disable it is if you wanted to define > using the infix method which doesn’t seem too likely:

julia> julia> x = 3
> (generic function with 1 method)

julia> "foo" > :bar
3

BTW if I want to have my own prompt and use this feature I have to use something like:

repl.interface.modes[1].prompt="julia> #=$VERSION=# "

Then I will have (not very nice prompt):

julia> #=1.0.1-pre.4=# a = 3
3

And if I copy+paste it works (but is even more ugly):

julia> #=1.0.1-pre.4=# #=1.0.1-pre.4=# a = 3
3

I would rather see something more flexible:

# maybe julia[any string without '>']>
julia 1.0.1-pre.4>
# or maybe julia([any string without '>'])>
julia( 1.0.1-pre.4 )>

This one also doesn’t work:

(v1.0) pkg> add package
# copy+paste it
julia> (v1.0) pkg> add package

ERROR: syntax: missing comma or ) in argument list
# or 
(v1.0) pkg> (v1.0) pkg> add package

ERROR: expected command. instead got [(v1.0)]

It is only active for julia>.

I just tried if this works at all under WIN11 (Julia 1.8.5) and failed.

image

Older windows terminals are too broken for this to work, strongly recommend using a newer terminal (which is the default in newer windows versions).

1 Like

I just tried it using the (relatively) new “Windows Terminal” and it still did not work. I assumed this is what you meant (as opposed to the old cmd or powershell terminals). Is Windows Terminal the new terminal you meant or is there something else instead?

It wasn’t working for me a year ago in Win 11 PowerShell, and it still doesn’t work with the latest PS update.

Hmm. @kristoffer.carlsson do you know why it doesn’t work on Windows?

See bracketed paste in conhost · Issue #395 · microsoft/terminal · GitHub. It should be supported.