I liked garyjohn's answer using RunningX as it is smart about bad but non-empty DISPLAY variables.
But.. I would like to go one better. I often use screen sessions. I might start a screen session at home, DISPLAY is defined. Then, I ssh in from elsewhere, DISPLAY is still defined and it actually displays... at home! So.. then I will 'export $DISPLAY='. This is fine but since that answer already checked for, and found a valid DISPLAY variable at the time .bashrc initially ran EDITOR would still be set to gvim.
I came here looking for a solution to a similar problem, I wanted to use yelp for X-GUI manpage browsing but automatically drop down to the traditional man command when not using X. Based on what I read here, I came up with this:
man () {
if RunningX
then
yelp "man:$@";
else
/usr/bin/man "$@";
fi
}
I'm thinking that for the EDITOR problem one could do something like follows:
EDITOR=vim
vim () {
if RunningX
then
gvim "$@";
else
vim "$@";
fi
}
I would have just posted that w/o the long story but I haven't tested it. I consider myself a bash newbie. I tested my 'man command', that works! I'm using emacs for my editor though, it does it's own check for X. I don't even have gvim installed.
if [[ $DISPLAY ]]
for the double bracket aficionados… – Serge Stroobandt – 2017-10-16T19:55:11.427