A shortcut to edit long shell commands in your $EDITOR
- Published at
- Updated at
- Reading time
- 2min
If you're wrangling long shell commands, tweaking parameters can be a pain. Especially when your command spans multiple lines and includes line breaks, you must be careful not to lose your changes.
Press one key too much, and you're shuffling through your shell history, and your current changes will be gone.
Today I learned that there's hope because there are shortcuts to quickly edit long shell commands in your favorite editor ($EDITOR
).
In bash
, if you want to edit an already typed command in your favorite editor, you can press ctrl + x
followed by ctrl + e
.
This combination triggers the so-called edit-and-execute-command
, which then creates a temporary file, and opens it in the editor defined in your shell's $EDITOR
variable. When you save and close this file — your terminal command will be updated. Pretty magical!
But what about zsh?
The default zsh
doesn't come with this shortcut, but you can quickly add it with some keybindings. Note that the function is called edit-command-line
in zsh
land.
autoload -U edit-command-line
zle -N edit-command-line
bindkey '^xe' edit-command-line
bindkey '^x^e' edit-command-line
If you're using oh-my-zsh
, these bindings are already set up for you.
Paweł pointed out that for fish
users the key combination is alt+V
.
Troubleshooting
For the VS Code users: you must append the --wait
flag to your EDITOR
declaration for the shortcut to work — EDITOR="code --wait"
.
Join 5.2k readers and learn something new every week with Web Weekly.