Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I tried to integrate MSYS2 shell into Visual studio Code integrated terminal. Here's my user settings:

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

However, I ran into a problem where --login changes the current working directory to Windows home. I want the current directory to be at the root of my workspace.

My further attempt was I tried add a flag -c 'cd ${workspaceRoot}'. However, the bash would crashed on start. I could properly get to current directory by removing --login, but without login mode, all other shell command (ls, cd, etc) are not available.

How do I properly integrate MSYS2 shell into my vscode?

like image 504
pbeta Avatar asked Aug 23 '17 10:08

pbeta


People also ask

How do I run C code in Visual Studio?

Download & Install the C/C++ Extension We need to click on the extension button that displays a sidebar for downloading and installing the C/C++ extension in the visual studio code. In the sidebar, type C Extension. In this image, click on the Install button to install the C/C++ extension.


2 Answers

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.

like image 71
Jānis Rūcis Avatar answered Oct 12 '22 14:10

Jānis Rūcis


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

like image 23
Ali80 Avatar answered Oct 12 '22 13:10

Ali80