Project Inquiry
Published on

Z Shell Tip: Edit Current Command Line with Vim

Editing long command lines in a shell is very slow and boring. Wouldn’t it be cool, if you could utilize the full power of Vim (or your editor of choice) instead? Well, it is not only possible, it is also incredibly easy to set-up …

Setting EDITOR

If you haven’t done so already, it is now time to set your EDITOR environment variable. For instance, I have the following line in my ~/.zshrc:

$ export EDITOR=vim # or emacs or nano or ...

EDITOR specifies the editor, zsh depends on for its the magical “edit command line” feature, you’ll see in a minute. Basically, all editors are fine for this.

Config for Z Shell

To configure Z Shell, append following snippet to your ~/.zshrc:

# Use ESC to edit the current command line:
autoload -U edit-command-line
zle -N edit-command-line
bindkey '\033' edit-command-line

With that in place you can press ESC to open Vim, which is automatically pre-filled with the content of your current command line. Edit the command as you like and quit Vim with :wq. Afterwards, you are back at the point where you started Vim, but your command line is changed :)

You may notice a small delay after pressing ESC and before your editor opens. Setting KEYTIMEOUT to a lower value fixes this problem. I’ve set the environment variable to 1 in my personal ~/zshrc:


Disclaimer: This post is about Z Shell – I know the same thing is possible with bash and I don’t care!
Software: Ubuntu v15.04, Z Shell v5.0.7, and Vim v7.4.