[ | Date | | | 2021-06-08 00:37 -0400 | ] |
Many programming language and database REPLs allow jumping out of their built-in line editor into a full-blown text editor. For example, psql
1 users can type meta-command \e
to edit the previous query:
The user already entered a simple query in psql and saw its output. They type \e
to further edit the query.
The default editor starts, with the last query loaded. In this case, this is GNU Nano, the default editor on Ubuntu 20. In this screenshot, the user has already altered the query, adding adequate indentation and naming the column n
. The user then saves and quits (in the case of this specific text editor, by typing Ctrl-o, and then, after confirming the file name, Ctrl-x).
Once the text editor exits, control goes back to psql, the SQL query is run, and its result gets displayed.
In the following list, C-x
means to press and hold the Ctrl key, then press and release key x
, then release Ctrl.
In your command line shell, set variable EDITOR
to the editor you would like to use. For Bash, that could mean appending a line such as the following to file ~/.bashrc
:
export EDITOR='emacs -nw'
This example makes Emacs in non-graphical mode the default2. Note that this also works with non-REPL tools that need to start text editors, such as Git.
With the change applied, the same \e
command results in the following popping up:
psql meta-command \e
now starts Emacs
In the first example above, I mentioned that, on my system, Nano was the default editor; i.e., it gets started even if both variables EDITOR
and VISUAL
are unset. This works through Debian's Alternatives system, which maintains a list of defaults as symbolic links. For example, on this system, we can see that editor
is a symbolic link to Nano:
$ readlink /etc/alternatives/editor
/bin/nano
The command to change those defaults system-wide is update-alternatives
:
# update-alternatives --config editor
There are 4 choices for the alternative editor (providing /usr/bin/editor).
Selection Path Priority Status
------------------------------------------------------------
* 0 /bin/nano 40 auto mode
1 /bin/ed -100 manual mode
2 /bin/nano 40 manual mode
3 /usr/bin/emacs 0 manual mode
4 /usr/bin/vim.tiny 15 manual mode
Press <enter> to keep the current choice[*], or type selection number: ^C
The REPL for database management system PostgreSQL. As of 2021-06, a sample public database is accessible by running:
psql postgres://reader:NWDMCE5xdipIjRrp@hh-pgsql-public.ebi.ac.uk:5432/pfmegrnargs
Note that there is a priority list of environment variables that typically get checked; an answer on the Unix & Linux Stack Exchange explains in detail what variables VISUAL
and EDITOR
are meant for. I have successfully been ignoring VISUAL
for decades now, leaving it unset and relying entirely on EDITOR
.
Additionally, REPLs often have their own specific rules about how they decide on a text editor to use. For example, psql supports PSQL_EDITOR
, EDITOR
, and VISUAL
, tried in this order, and defaulting to vi
on Unix. Other REPLs may support choosing a text editor in a configuration file.↩
Quick links: