Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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

share|improve this question

2 Answers 2

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.

share|improve this answer

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

if [ "$DISPLAY." == "." ]; then
 export EDITOR='vim'
else
 export EDITOR='gedit'
fi
share|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
2  
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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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