2

Can one detect what terminal application is being used? I want the actual application, not TERM env in the question.

I'd like to detect iTerm 2 / Terminal.app so I could set OSX specific keyboard mappings, otherwise PC.

(See ALT+arrow moving between words in zsh and iTerm2 )

3 Answers3

1

See this answer: https://stackoverflow.com/questions/7052683/how-can-i-identify-the-current-terminal-emulator-from-a-bash-script/7053241#7053241

Not every program sets these variables, but echo $TERM_PROGRAM at least works in Terminal.app, iTerm2, and WezTerm. It does not work with Alacritty, but that's pretty typical of their ultra-minimalist feature set.

mattmc3
  • 184
1

You need to SSH forward local environment variables, as explained here:

http://groups.google.com/group/iterm2-discuss/msg/7cc214c487d31bc8

0

I made the following script:

#!/bin/bash

pid=$$ # Current PID
ps -f $$ | head -n 1 # Show the header of ps
while [ $pid -gt 0 ]; do # No more parent when we reach 0 (the kernel)
        ps -f $pid | tail -n +2 # ps current pid and remove header
        pid=$(ps -o ppid $pid|tail -n 1) # Get parent pid
done

It takes the current PID ($$ in bash) and recursively gets the parent PID until we reach 0 (which is the kernel), printing the ps -f output along the way (and a header to start, with ps -f | head -n 1)

Two limitations I can think of:

  1. If run over SSH, the parent will be sshd and not the graphical terminal application.
  2. If executed in a separate script it will print that script too.

However you should be able to grep its output and detect if one of the parent process is iTerm.app or Terminal.app when run locally.

Calimo
  • 1,465