When I hit Ctrl+x, Ctrl+e in zsh, I can edit the current command line in by $EDITOR or $VISUAL. However, I'd like to use nano, and to get syntax highlighting for shell syntax there, I have to pass -Y sh, as nano doesn't recognise shell syntax automatically when editing the command line (zsh creates /tmp/random-name without a .sh extension to pass to nano).

I can execute

EDITOR='nano -Y sh'

and then press Ctrl+x, Ctrl+e to get the desired result. However, other programs use $EDITOR/$VISUAL, too. If I set $EDITOR/$VISUAL as above, and then do (for example) git commit, the commit message is highlighted as shell syntax, which I want to avoid.

I also tried

EDITOR='nano -Y sh' fc

which did work, however that seems a little verbose to type out each time (I might put it in a function though). Also, fc prepopulates the command line with the last history command line, and to use it, I have to type out the command. That means I could not type out some long command in zsh and then decide to edit it in nano as I could with the keyboard shortcut.

So, is there a way for me to tell zsh the editor/flags to use only for editing the command line when pressing Ctrl+x, Ctrl+e that other programs ignore? I would love some environment variable that I can set in ~/.zshrc and then forget about.


The universal way to solve every computer problem¹ is to add a level of indirection.

Instead of calling edit-command-line, call a wrapper function.

nano-command-line () {
  local VISUAL='nano -Y sh'
zle -N nano-command-line
bindkey '^X^E' nano-command-line

¹ Hyperbole.


The zle script uses EDITOR or VISUAL. One method would be to copy that script from wherever it hides in $fpath into a custom functions directory, edit the script to use a different environment name, then ensure that the custom functions directory is called before the installed-with-zsh one.

# in .zshrc, set something like
fpath=(~/.zsh/functions $fpath)

# then in the shell, something like
mkdir -p ~/.zsh/functions
cp /usr/share/zsh/*/functions/edit-command-line ~/.zsh/functions

Then edit up the edit-command-line file and specify a different environment variable.

  • Good idea, thanks! I'll wait a while to see if something else comes up before accepting your answer, if that's alright. – Timo Feb 29 '16 at 21:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.