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

link|improve this question
feedback

2 Answers

up vote 2 down vote accepted

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.

link|improve this answer
feedback

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

if [ "$DISPLAY." == "." ]; then
 export EDITOR='vim'
else
 export EDITOR='gedit'
fi
link|improve this answer
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. – whitequark Aug 18 '10 at 0:02
1  
if [ -z $DISPLAY ] – Dennis Williamson Aug 18 '10 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). – Gilles Aug 18 '10 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. – Dennis Williamson Aug 19 '10 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. – Dennis Williamson Aug 19 '10 at 16:16
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.