How to use your favorite text editor to edit queries interactively

Index, feed.

[ Date | 2021-06-08 00:37 -0400 ]

Starting a text editor from within a command shell

Many programming language and database REPLs allow jumping out of their built-in line editor into a full-blown text editor. For example, psql1 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 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).

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.

Once the text editor exits, control goes back to psql, the SQL query is run, and its result gets displayed.

Commands for some shells and REPLs

In the following list, C-x means to press and hold the Ctrl key, then press and release key x, then release Ctrl.

How to choose a different editor

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

psql meta-command \e now starts Emacs

On Debian-based systems

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

  1. 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
  2. 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.

www.kurokatta.org


www.kurokatta.org

Quick links:

Photos
Montréal
Oregon
Paris
Camp info 2007
Camp Faécum 2007
--more--
Doc
Jussieu
Japanese adjectives
Muttrc
Bcc
Montréal
Couleurs LTP
French English words
Petites arnaques
--more--
Hacks
Statmail
DSC-W17 patch
Scarab: dictionnaire de Scrabble
Sigpue
Recipes
Omelette soufflée au sirop d'érable
Camembert fondu au sirop d'érable
La Mona de Tata Zineb
Cake aux bananes, au beurre de cacahuètes et aux pépites de chocolat
*