Using msys shell in Visual Studio Code

aNew picture aNew · Apr 9, 2017 · Viewed 9.2k times · Source

I'm getting into Visual Studio Code as I like its lightweight design and flexibility. It has an integrated terminal that can run, I believe, any shell [see here]. I'd like to use it run the msys shell that I've been using lately. Setting the terminal to run another shell other than the default powershell.exe should be as simple as changing the settings as described [here]. However, I use the msys shell by running the msys.bat file, as recommended on mingw.org.

So my question is, where is the .exe file I need to set the terminal to the msys shell? Or is running this shell without the .bat in this fashion impossible? Thanks!

Answer

Jonny Morrill picture Jonny Morrill · May 5, 2017

According to the msys.bat script, the actual executable that is launched depends on your TTY settings. The default setting uses the native command prompt and launches the sh.exe file as you can see from the following snippet:

:startsh
if NOT EXIST %WD%sh.exe goto notfound
start %WD%sh --login -i
exit

To get this to work in Visual Studio Code, you will need to add the following user settings:

"terminal.integrated.shell.windows": "C:\\MinGW\\msys\\1.0\\bin\\sh.exe",
"terminal.integrated.shellArgs.windows": ["--login", "-i"]

The path to the sh.exe file may be different depending on your install location for MSYS.

EDIT: (2019-01-20)

While the above still works for MSYS v1.0, I have since switched over to MSYS2 (https://www.msys2.org/). You can use the following settings to setup your Visual Studio Code to work with MSYS2 just as for v1.0 (once again, your install location might be different than mine):

"terminal.integrated.shell.windows": "C:\\msys64\\usr\\bin\\bash.exe",
"terminal.integrated.shellArgs.windows": ["--login", "-i"]

For extra-credit, we are going to modify an environment variable so that we always open the terminal in the current directory. There are various ways to accomplish this via scripting etc. However, the simplest way to do it, in my opinion, is to use the CHERE_INVOKING environment variable. By setting this flag to 1 it will inform the shell to use the current directory as the default entry point. Here is a complete tutorial on how to enable this flag:

  1. Press the windows key and type run and hit enter
  2. In the run dialog, type the following:

    rundll32.exe sysdm.cpl,EditEnvironmentVariables
    

    Run Dialog to open Environment Variables

  3. In the Environment Variables dialog that is opened, add a new user variable called CHERE_INVOKING and set it's value to 1.

    enter image description here

With this flag enabled in the Windows system, it should automatically open the terminal from the location where you called the bash.exe executable. For Visual Studio Code, this will be your root project directory. Enjoy!