How do I integrate MSYS2 shell into Visual studio code on Window?

To inhibit the working directory change from your current directory, set the CHERE_INVOKING environment variable to a non-empty value:

    "terminal.integrated.env.windows": {
        "CHERE_INVOKING": "1"
    },

In MSYS login scripts, setting the CHERE_INVOKING variable serves only to prevent a shell from doing a cd "${HOME}", and nothing else.

If you require a MinGW toolchain, set the MSYSTEM environment variable to select a toolchain. Recognized values are MSYS (default), MINGW32 or MINGW64.

    "terminal.integrated.env.windows": {
        "MSYSTEM": "MINGW64",
    },

In full, the VS Code settings might look like so:

{
    "terminal.integrated.shell.windows": "C:\\msys64\\usr\\bin\\bash.exe",
    "terminal.integrated.shellArgs.windows": [
        "--login",
    ],
    "terminal.integrated.env.windows": {
        "CHERE_INVOKING": "1",
        "MSYSTEM": "MINGW64",
    },
}

To provide some context on the very cryptic nomenclature of CHERE_INVOKING: chere is apparently a Cygwin command for installing and managing a "Command Prompt Here" folder context menu item. Although MSYS2 inherits the environment variable from Cygwin, it doesn't actually inherit the command itself.


Answers here are from the old method which is now (July 2021) deprecated in VSCode the new suggested way, add this to settings.json:

"terminal.integrated.profiles.windows": {
    "PowerShell": {
      "source": "PowerShell",
      "icon": "terminal-powershell"
    },
    "Command Prompt": {
      "path": [
        "${env:windir}\\Sysnative\\cmd.exe",
        "${env:windir}\\System32\\cmd.exe"
      ],
      "args": [],
      "icon": "terminal-cmd"
    },
    "Git Bash": {
      "source": "Git Bash"
    },
    "MSYS2": {
      "path": "C:\\msys64\\usr\\bin\\bash.exe",
      "args": [
        "--login",
        "-i"
      ],
      "env": {
        "MSYSTEM": "MINGW64",
        "CHERE_INVOKING": "1"
      }
    }
  },

reference: integrated-terminal#_configuring-profiles


This works for me:

{
    "terminal.integrated.shell.windows": "C:\\msys64\\usr\\bin\\bash.exe",
    "terminal.integrated.shellArgs.windows": ["--login", "-i"],
    "terminal.integrated.env.windows":
    {
        "MSYSTEM": "MINGW64",
        "CHERE_INVOKING":"1"
    }
}

Original but not working 100% (accepted as the answer)

This will start the MSYS2 bash shell properly so your .bash_login gets executed:

"terminal.integrated.shell.windows": "C:\\msys64\\msys2_shell.cmd",
"terminal.integrated.shellArgs.windows": ["-defterm", "-mingw64", "-no-start", "-here"]

Edit

The original answer seemed to work at the time, but when I tried to start using tasks in VSCode it was clearly not working. Trying to run a task that simply called make all caused the following error:

/usr/bin/bash: /d: No such file or directory
The terminal process terminated with exit code: 127

From the other answers, using "terminal.integrated.shellArgs.windows": ["--login", "-i"] got the almost the correct environment (MSYS instead of MINGW64) but started in the wrong directory, and "terminal.integrated.shellArgs.windows": ["-lic", "cd $OLDPWD; exec bash"] started in the correct directory with the correct environment but could not run tasks.

I came up with this solution that so far seems to work fine.
In VSCode settings:

"terminal.integrated.shell.windows": "C:\\msys64\\usr\\bin\\bash.exe",
"terminal.integrated.env.windows":
{
    "MSYSTEM": "MINGW64",
    //"MSYS2_PATH_TYPE": "inherit",
    "MSVSCODE": "1"
},

In .bashrc:

if [ ! -z "$MSVSCODE" ]; then
    unset MSVSCODE
    source /etc/profile
    cd $OLDPWD
fi