Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check whether shell scripts / Vim running in VS Code integrated terminal?

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?

like image 357
yongjieyongjie Avatar asked Aug 24 '19 06:08

yongjieyongjie


People also ask

How do I run a shell script in Visual Studio code?

Open Visual Studio Code and press and hold Ctrl + ` to open the terminal. Open the command palette using Ctrl + Shift + P . Type - Select Default Shell . Select WSL Bash (NOT Git Bash ) from the options.

How do I know if VS Code is running?

To run or debug a simple app in VS Code, select Run and Debug on the Debug start view or press F5 and VS Code will try to run your currently active file.

Can I use Vim in VS Code?

So, here’s how to set up Vim in VS code and what commands you should know. — and don’t worry, you won’t get lost. The advantage of the VS Code extension is that you can use Vim, but you don’t have to. Have fun!

How do I open the integrated shell in Linux terminal?

To enable the integrated shell, just use the ctrl +` keyboard shortcut (that’s control backtick, not apostrophe). This should launch your default shell. If you’re on Windows, this will be a standard command prompt, but you can adjust it to use Powershell or even Bash if you’d like.

How do I test my terminal in VS Code?

Test your shell directly. Try running your designated integrated terminal shell outside VS Code from an external terminal or command prompt. Some terminal launch failures may be due to your shell installation and are not specific to VS Code.

How to access a shell in a Vim session?

For this, all you have to do is to run the following command from the editor: and type 'exit' when you are done with the shell work - this will bring you back into the Vim session from where you left initially. While the ability to access a shell definitely has its own uses in real world, it can also be used as a privilege escalation technique.


1 Answers

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
like image 100
jmt Avatar answered Nov 15 '22 07:11

jmt