9

Issue

Question is as stated in the title.

In gist, I am trying to get my bash scripts and Vim to behave differently when running in VS Code's integrated terminal.

Things I've found

I have managed to find several other Stack Overflow questions, but they relate to detection of operating systems:
- How to check if running in Cygwin, Mac or Linux?
- How to detect the OS from a Bash script?

yongjieyongjie
  • 813
  • 10
  • 18
  • Environment variables are the best way to do that... Can you configure the shell in VS Code to export a unique environment variable you can use for detection? Or can you check the environment in your VS Code shell to see if it already includes a variable you could use for this detection? – filbranden Aug 25 '19 at 18:17

2 Answers2

21

By examining the shell variables in the vscode terminal you can see that it sets TERM_PROGRAM=vscode. In my .bash_profile I have the following and it works great:

if [ "$TERM_PROGRAM" == "vscode" ]; then
    # some stuff
else
    # other stuff
fi
jmt
  • 411
  • 4
  • 6
0

You can check the parent process until it matches or you find PID==1

#! /bin/bash

pid=$$
until (( pid == 1 ))
do
    [[ $(ps -o command= -p $pid) =~ Code ]] && break
    pid=$(ps -o ppid= $pid)
done

(( pid != 1 )) && echo "VS Code"

Check man ps in case yours have different options

Diego Torres Milano
  • 65,697
  • 9
  • 111
  • 134