3

I am trying to keep gvim as my default editor when I use VNC to my machine. But want to keep vim as my default editor when I am logged in through SSH. I am not sure how to differentiate in my .bashrc file to do this automatically. Similarly, is there a way I can know the session is through a console directly connected with my machine.

Thanks

adikshit
  • 205
  • 3
  • 10

3 Answers3

2

It sounds like you don't actually care whether it's SSH, VNC or console. You care whether you have a GUI to run gvim on or not.

You can check this with $DISPLAY:

[[ $DISPLAY ]] && export EDITOR=gvim || export EDITOR=vim

This will set your editor to gvim for VNC as well as XDMCP, NX, Chromoting, local graphical logins, ssh with graphical forwarding, and anything else with an X11 display.

It will set your editor to vim for regular SSH logins as well as Telnet, rsh, serial consoles, local logins, and all other non-X11 based logins.

that other guy
  • 116,971
  • 11
  • 170
  • 194
0

The environment variable $SSH_AUTH_SOCK should be set in any SSH session. So in your .bashrc, you could have

if [ -n "$SSH_AUTH_SOCK" ]
then
    # code for SSH sessions here
fi

As for determining whether you're on a directly attached console, see this question; to get the tty name in the shell (rather than from within a C program), use the tty command.

Community
  • 1
  • 1
ajk
  • 1,045
  • 5
  • 9
  • 1
    In my case I see that when I use VNC the SSH_AUTH_SOCK is set but not if I login through SSH. VNC Session: $ set | grep SSH SSH_AGENT_PID=4958 SSH_AUTH_SOCK=/tmp/keyring-TnDwmJ/ssh SSH_CLIENT='192.168.1.1 60038 22' SSH_CONNECTION='192.168.1.1 60038 192.168.1.2 22' SSH_TTY=/dev/pts/2 SSH Session : $ set | grep SSH SSH_CLIENT='192.168.1.1 49874 22' SSH_CONNECTION='192.168.1.1 49874 192.168.1.2 22' SSH_TTY=/dev/pts/2 Note: I am not sure if it is relevant but I am using putty for SSH and RealVNC for vnc connection – adikshit Sep 25 '13 at 20:44
  • Ah, it sounds as though your VNC session is being forwarded over an SSH tunnel. And I made the (inappropriate) assumption that you had SSH forwarding enabled for your other SSH session. Did you check the environment of both sessions for any other differences? Maybe there's something reliable you could use. – ajk Sep 25 '13 at 21:47
0

Thanks for the hint from ajk I looked into the variables set and saw that VNCDESKTOP is set only in case of vnc. I used the way suggested above and it works now.

Though I still hope someone can suggest if this is a complete solution or not

adikshit
  • 205
  • 3
  • 10