2

I need to check in my .vimrc whether or not the terminal in which vim is opened is running in Guake. How can I see this? The $TERM variable does not seem to be quite right...

  • Exactly what is guake? Is it a process? – DisplayName Nov 28 '14 at 13:42
  • 1
    @DisplayName Guake is a graphical terminal emulator for GNOME. – jw013 Nov 28 '14 at 17:16
2

There is no foolproof way. TERM uses the same value for mostly-compatible emulators; most modern terminal emulators aside from screen and tmux report xterm.

You can explore the process list to find Vim's parent's parent. That will tell you in what terminal emulator (if any) Vim is running in in the common case where Vim was launched from a shell which was started directly in the terminal emulator. You can even be a little smarter and traverse the process list, going from Vim to its parent, then its parent's parent, and so on, and stopping when you find a process that isn't running on the same terminal as its child: that process is probably a terminal emulator.

This heuristic can be fooled in a number of ways — in particular because it is possible (though uncommon) to start a program from anywhere and make it interact in another terminal by redirecting its input and output.

Here's a lightly-tested script which shows the command line of the terminal emulator that the current process is running on, assuming that the process is started as a descendant of that terminal emulator with no redirection shenanigans.

#! /bin/sh
set -f
pid=$PPID
my_tty=$(ps -p $$ -o tty=)
while
  [ "$pid" -ne 1 ] &&
  set -- $(ps -p "$pid" -o ppid= -o tty= -o args=) &&
  [ "$2" = "$my_tty" ]
do
  pid=$1
done
shift; shift
printf '%s\n' "$*"

Call this script which-terminal-emulator. In your .vimrc, you can add

let terminal_emulator=system('which-terminal-emulator')
if terminal_emulator =~ '.*guake.*$'
  …
endif

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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