Add Open Current Command Line Prompt In Editor as a Zsh TIL

This commit is contained in:
jbranchaud
2026-07-28 17:51:39 -05:00
parent 2c247f2e20
commit 0fbf06a2ce
2 changed files with 39 additions and 1 deletions
+2 -1
View File
@@ -10,7 +10,7 @@ working across different projects via [VisualMode](https://www.visualmode.dev/).
For a steady stream of TILs, [sign up for my newsletter](https://visualmode.kit.com/newsletter).
_1842 TILs and counting..._
_1843 TILs and counting..._
See some of the other learning resources I work on:
@@ -2171,6 +2171,7 @@ If you've learned something here, support my efforts writing daily TILs by
- [Create And Jump Into A Directory](zsh/create-and-jump-into-a-directory.md)
- [Link A Scalar To An Array](zsh/link-a-scalar-to-an-array.md)
- [List Available Zle Keybindings](zsh/list-available-zle-keybindings.md)
- [Open Current Command Line Prompt In Editor](zsh/open-current-command-line-prompt-in-editor.md)
- [Use A Space To Exclude Command From History](zsh/use-a-space-to-exclude-command-from-history.md)
## Usage
@@ -0,0 +1,37 @@
# Open Current Command Line Prompt In Editor
The [`fc` builtin](/unix/fix-previous-command-with-fc.md) is a nice trick for
being able to open my default editor with the previous command loaded in. I can
make quick edits, save and quit, and the updated previous command will populate
my prompt.
What if I want to work from a blank prompt or I want to edit the command I've
already started typing out?
[`zsh`](https://github.com/zsh-users/zsh) has a solution for this via a contrib
widget, `edit-command-line`, that ships with `zsh`. I can load this widget and
then bind it to something handy like `Ctrl-x Ctrl-e`. To do that, I can run the
following sequence of commands and even add them to my `zsh` config:
```bash
autoload -Uz edit-command-line
zle -N edit-command-line
bindkey '^X^E' edit-command-line
```
The `autoload` line creates a function stub with the name `edit-command-line`
(try `which edit-command-line` after running just that and you'll see what I
mean). The `zle` line binds the `edit-command-line` widget to the function named
`edit-command-line` (if they had different names, it would be `zle -N <widget> <function>`). Lastly, I bind `Ctrl-x Ctrl-e` to that `edit-command-line`
function.
When this runs, it will first try for the `VISUAL` editor, otherwise fallback
`EDITOR`. I happen to have both of those set to `nvim` in my config. When I hit
`Ctrl-x Ctrl-e`, `nvim` opens with the current command populated. I can make
edits, and then when I run `:wq`, the updated command replaces what was there. I
still have to hit enter to execute it.
I find this most useful when I'm typing a multi-line prompt, especially one
where I'm trying to get all the details right and I need to copy in values from
somewhere else. It is also invaluable for writing out [a command like this](/llm/include-a-file-with-message-to-ant.md) that has a real-world cost if
I accidentally bump enter too soon.