3

I can set my EDITOR variable in my .bashrc to e.g. gedit, which is a nice graphical editor on ubuntu. But when I log in over PuTTY that editor will fail.

What is the best way to detect if I'm logged in and can run a graphical editor, or not, and set EDITOR appropriately??

2 Answers 2

2

In ~/.xinitrc (or ~/.xprofile if X starts via a display manager):

export EDITOR=gedit
export VISUAL=$EDITOR

In .bashrc (or whatever user init file for your shell):

export EDITOR=emacs
export VISUAL=$EDITOR

I try to keep X related stuff out of my shell init files.

3

... some fiddling ... this may be it?

if [ "$DISPLAY." == "." ]; then
 export EDITOR='vim'
else
 export EDITOR='gedit'
fi
5
  • Or you can check $SSH_CLIENT, but this approach will work when you'll login into a plain tty, too, so it's more flexible.
    – Catherine
    Aug 18, 2010 at 0:02
  • 2
    if [ -z $DISPLAY ] Aug 18, 2010 at 1:47
  • @Dennis: why play with fire? if [[ -z $DISPLAY ]] in bash/ksh/zsh or if [ -z "$DISPLAY" ] to be portable (or if [ x"$DISPLAY" = x ] if you're paranoid about buggy antique Bourne shells). Aug 18, 2010 at 22:38
  • @Gilles: Because -z by definition knows how to deal with null and unset variables. However, you're correct insofar as it's better to have consistent habits. Aug 19, 2010 at 0:58
  • @Gilles: Thanks. I always use double brackets in Bash anyway. I never thought about the -o type of cases inside single brackets. Aug 19, 2010 at 16:16

You must log in to answer this question.

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