editing shell commands in $EDITOR

after reading Entering text in the terminal is complicated i was motivated to figure out a problem that has long vexed me: how do i edit a long command in my editor instead of in an interactive shell? for the last ~15 years i’ve just been writing stuff into a files and running them, and having those files laying around feels like finding cigarette butts on the beach.

it turns out shells support this!

fish shell

Alt+E edit the current command line in an external editor. The editor is chosen from the first available of the $VISUAL or $EDITOR variables.

also see the bit below around using the alt key on macs.

bash

edit-and-execute-command (C-x C-e)

Invoke an editor on the current command line, and execute the result as shell commands. Bash attempts to invoke $VISUAL$EDITOR, and emacs as the editor, in that order.

thanks to Julia Evans for motivating me to think about this, and to A Lloyd Flanagan for listening to me figure this out.

using the alt key in mac terminals

in terminal.app you’ll need to enable ‘Use Option as Meta key’

vscode’s terminal needs the following:

"terminal.integrated.macOptionIsMeta": true
© 2024 peter schilling