An editor in your REPL
One of my favorite features offered by some REPLs is the ability to pop open my
default editor. This is a great way to prepare a statement or series of
statements without worrying that I might bump <enter> too soon. Ruby's pry
whether used alone or as an IRB-replacement for rails console offers this via
edit. PostgreSQL's psql offers this via \edit. As well as a couple other
examples I'll mention at the end.
My Workflow
There are two ways this shows up in my workflow:
- I'm preparing a long statement or series of statements and I want to be able to use the features of my editor to navigate around and make edits in a way that a REPL cannot support. I also want do that work without worrying that I might accidentally run the statement too soon.
- I need to make an adjustement to my most recent statement, for instance when there was a typo in a really long line. I can run the edit directive and it will open the most recent statement (or series of statements) in my editor.
My default editor is nvim (Neovim). This is set via both EDITOR and VISUAL
env vars in my
dotfiles ↗.
These edit commands work seamlessly with nvim (and other CLI-based editors)
because the REPL is interrupted by the editor process and waits until that
completes. When I'm ready to execute whatever I've prepared in the editor, I
save and quit (:wq). It will be executed as is right then.
GUI Editors
If your default editor is something like VS Code (or any of its derivatives),
Sublime Text, Zed, etc. -- namely, a separate app that is a GUI, then this won't
work as is. These GUI editors typically support some kind of --wait flag with
their CLI binary that can be used to block the REPL process.
You can try it out like so:
VISUAL='code --wait' rails console
Once you save the file and close that editor tab (not necessarily the entire editor), the REPL will be able to resume by executing whatever you saved in that temporary file.
Other Examples
Node is another example that kind of supports this functionality. Running
.editor from an active Node REPL session, you'll get an inline prompt where
you can enter multiple lines and then either execute (CTRL-D) or bail
(CTRL-C). I'd rather it just handed me off to my EDITOR. Noteably, it
doesn't pull in your most recent statement and it doesn't allow you to edit the
previous lines in the current editor mode instance.
Bash supports this behavior. Pull the current state of your shell prompt into
your default editor by hitting Ctrl-X Ctrl-E. This works just like the pry
and psql examples. Unfortunately, this doesn't seem to also be supported by
zsh.
As a partial backup, since I prefer zsh, I can use
fc ↗.
This is a shell built-in, available in both bash and zsh, that will open
your most recent command in your default editor.
Lastly, Claude Code lets you jump into your default editor when typing up a
prompt by hitting CTRL-g. I use this all the time when I want include and
format a code block.
Broader Support
I'd like to see this functionality supported more widely across popular REPLs.
Ruby's irb and Python's python both don't support this. I wish they did.
If your REPL doesn't support this, it should. Ask for it!