PSA: Inserting a new line in the REPL


#1

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

#2

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.


#3

Alt-Enter is usually easier to reach.


#4

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:


#5

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?


#6

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


#7

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…