PSA: Inserting a new line in the REPL

This tip may help someone - I’ve found it useful myself :slight_smile:

Ever wanted to insert a new blank line into something you’re working on at the REPL?
Just pressing <Enter> is going to execute the command!

TL;DR Use: <Esc> followed by <Enter> (or <Alt><Enter> works too, which may be easier to type)

Note: this trick works mid-line to break a long line, too!

(on macOS to get <Alt><Enter> working it may require an option to be set in your terminal application - look for options about the “Meta-key”)

For example, suppose you’ve just typed/pasted the following definition for the function f:

julia> function f(x, y)
           x + y
       end

But you’d like to insert another line above x + y.

Press the <Up> key to get back what you previously typed, and move the cursor to the start of that line (or the end of the preceding line (denoted by ):

julia> function f(x, y)
       █   x + y
       end

then press the <Esc> key followed by <Enter>, giving you:

julia> function f(x, y)
       █
           x + y
       end

at which point you can make your edit:

julia> function f(x, y)
           y = y >= 0 ? sqrt(y) : 0
           x + y
       end
28 Likes

Other useful readline keys:

  • <Ctrl><k>: kill (cut) to the end of the line
  • <Ctrl><y>: yank (paste) what was last killed (cut)
  • <Ctrl><w>: kill (cut) the preceding word
  • <Ctrl><r> substring: recursively search through your history for substring.
    Keep hitting - <Ctrl><r> until you get the right one if there are multiple matches.

See the docs on Julia’s REPL key bindings for more gems.

5 Likes

Alt-Enter is usually easier to reach.

1 Like

Good point - I’m on a Mac, but I’ve never been able to get <Alt><Enter> to work, though I have mapped <CapsLock> as <Esc> :slight_smile:

1 Like

I note that shift-enter is almost universal in the internet age/web related apps for this purpose… plus I thought the goal was to be a bit more modern than readline?

2 Likes

Alt-Enter does not work on windows as it is used to toggle fullscreen.

I second the proposal for Shift-Enter or Ctrl-Enter (Notebook-Style).

4 Likes

See https://github.com/JuliaLang/julia/issues/18295.

I never managed to do the keyboard customization suggested in that issue. Would be great if someone took a crack at that, it makes the REPL quite difficult to use on Windows…

I’m used to that yank means copy (from vim) are you sure that y stands for yank?

https://docs.julialang.org/en/v1/stdlib/REPL/#Key-bindings-1

(then search for yank for explanations)

EDIT: Oh, and I should have said welcome!

I see the documentation says so, but the use of the word is opposite of what it means i vim/vi. I think other people will find this confusing as well. In the dictionary it is defined as: pull with a jerk. Sounds more like copying to me.

Maybe it should be changed to p for paste for example.

“Yank” and the key-binding is consistent with the terminology used in Emacs and Readline for the same action:

https://www.gnu.org/software/emacs/manual/html_node/emacs/Yanking.html
https://www.gnu.org/software/bash/manual/html_node/Readline-Killing-Commands.html

That said, I’m a Vim user and it would be great to see vi keybindings in the Julia REPL (where y is a copy-action) :slight_smile:

2 Likes

Indeed, vim modes would be awesome.

1 Like

Decades long Emacs guy: Ctrl-y rules :grinning:

3 Likes

Just in case anyone stumbles across this old thread again (like I did):
Alt+Shift+Enter for a new line in Julia REPL works under Windows

2 Likes

Yep, It works, but move line and ident line commands alt + arraows did not workd