I'm using Zsh and some CLI's such as Google's gcloud load files via $EDITOR.

For me, I have set this as:

export EDITOR="/usr/local/bin/mvim"

With aliases for that editor using the -v flag which loads vim in the terminal:

 mac_vim="$EDITOR -v"
 alias vi=$mac_vim
 alias vim=$mac_vim
 alias v=$mac_vim

That works but when the gcloud CLI calls $EDITOR it does not have the -v flag so it opens in a new terminal. I thought I could change that by instead adding the -v flag here:

export EDITOR="/usr/local/bin/mvim -v"

However, when I try and run that on the command line I get:

$EDITOR text.html
zsh: no such file or directory: /usr/local/bin/mvim -v

Is it possible to include a flag when exporting my $EDITOR?


Use a wafer-thin shell wrapper:

exec /usr/local/bin/mvim -v "$@"

And stash that somewhere (somewhere in PATH or maybe in ~/libexec or whatever) and then set that wrapper as the EDITOR value.

With ZSH, one could force the expansion:

% EDITOR='echo -E'
% $EDITOR /etc/passwd
zsh: command not found: echo -E
% $=EDITOR /etc/passwd

As detailed in zshexpn(1):

          Perform  word splitting using the rules for SH_WORD_SPLIT during

Note that ZSH contradicts other shells on the matter of not doing SH_WORD_SPLIT by default. The POSIX default, by the way, is a mighty fine rake that many have used incorrectly and thus have stepped on and been whacked by:

% DERP="/var /nish"
% ls $DERP
ls: cannot access /var /nish: No such file or directory
% sudo bash
# DERP="/var /nish"
# rm -rf $DERP 

However various other programs, possibly ones that do not know how to perform shell word splitting, may call EDITOR. The most portable option for EDITOR is therefor something that can be executed directly by an exec(3) call, and if necessary that script can add any flags for the editor actual. Hence, a wafer-thin wrapper.

  • That makes sense, I'd like to understand why I can't add the flag as part of the export. Any explanation on that piece? – Anthony Mar 8 '17 at 16:30
  • 5
    Whether this works depends on how the application in question calls $EDITOR. The simple way is to treat it as a single string pointing to a file, and this is what gcloud apparently does. For flags in the export to work, the application would have to parse the string like a shell does and pass the words as separate arguments in argv. – Tom Hunt Mar 8 '17 at 16:33

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.