Published at
Updated at
Reading time
2min
This post is part of my Today I learned series in which I share all my web development learnings.

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).

Use your editor to edit long commands (bash)

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!

Editing of a command using `ctrl + x` and `ctrl + e`

But what about zsh?

Use your editor to edit long commands (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.

Use your editor to edit long commands (fish)

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".

If you enjoyed this article...

Join 5.5k readers and learn something new every week with Web Weekly.

Web Weekly — Your friendly Web Dev newsletter
Reply to this post and share your thoughts via good old email.
Stefan standing in the park in front of a green background

About Stefan Judis

Frontend nerd with over ten years of experience, freelance dev, "Today I Learned" blogger, conference speaker, and Open Source maintainer.

Related Topics

Related Articles