How to set the editor command to use *only* for edit-command-line in zsh

zsh

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'
VISUAL="$EDITOR"

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.

Best Answer

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'
  edit-command-line
}
zle -N nano-command-line
bindkey '^X^E' nano-command-line

¹ Hyperbole.

Related Question