Editing the command line in Vim

Constructing long commands on the command line can quickly get out of control. I use Vim keybindings in ZSH, which helps greatly. They allow you to navigate the shell using familiar shortcuts from Vim. Wouldn't it be neat if you could edit those commands directly in Vim though?

You can.

Add this to your ~/.zshrc file and reload your terminal.

autoload edit-command-line
zle -N edit-command-line
bindkey -M vicmd v edit-command-line

Now when you hit ESC and then v, the current command line will open in Vim. Saving and exiting (:wq) will return you to the command line with the changes you made.