Bashrc – How to Check if X Window is Available

bashbashrcgvimvimxorg

There is a part in my ~/.bashrc, which sets $EDITOR to be gvim. It works fine when I am in X window. However, if I ssh to my workstation (from another workstation) gvim starts to complain "cannot open display".

Therefore I wish to put an if statement in bashrc, that if X window is available then use gvim, otherwise let it be vim.

How could I achieve that?

Best Answer

One way to do this tests the existence of the DISPLAY environment variable:

if [ "$DISPLAY" ]
then
    EDITOR=gvim
else
    EDITOR=vim
fi

In some cases, DISPLAY will be set even though your gvim won't be able to contact the X server. In those cases, use the RunningX program:

if RunningX
then
    EDITOR=gvim
else
    EDITOR=vim
fi
Related Question