10

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?

6 Answers 6

11

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
1
7

The $DISPLAY environment variable should be set to something if you are in X, and unset if you are not connected to a display.

So you could use this:

if [ -n "$DISPLAY" ]; then
    alias vim='gvim'
else
    alias vim='vim'
fi

But there is another solution. It's called ssh X forwarding.

Run

ssh -X yourworkstation

then you can run vim on a machine you are ssh'd into, and it will display on the machine where you are running X.

3

Put this into your .bashrc:

if [ -z "${DISPLAY:-}" ]; then
  EDITOR='vim'
else
  EDITOR='gvim'
fi
1

While this is not an answer to your question, I thought it maybe useful to point out that if you're sshing from one workstation running X* to another workstation, you actually can use gvim if you setup X forwarding with ssh. The window information will just be sent over the network and gvim will just pop up like it would if run locally.

Add the following in ~/.ssh/config

Host *
   ForwardX11 yes 

Now you'll be able to just run gvim.

If you're workstation of origin is Windows and not running X, you actually CAN startup an X server locally using Cygwin. Once that's setup, you can configure popular ssh clients like PuTTy to do X11 forwardning, and then gvim will pop up in what appears to be a native windows-decorated window.

0

Just check one of environment variables that Xorg sets on startup, like XLOCAL that contains information about current X11 session.

0

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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .