How do I integrate MSYS2 shell into Visual studio

2020-02-07 17:26发布

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?

8条回答
Bombasti
2楼-- · 2020-02-07 17:37

The accepted answer works for me but with zsh shell ("terminal.integrated.shell.windows": "C:\\msys64\\usr\\bin\\zsh.exe"), things are much slower than with bash. I don't know why but using ConEmu's cygwin/msys terminal connector greatly helped:

"terminal.integrated.shell.windows": "C:\\conemu\\ConEmu\\conemu-msys2-64.exe",
"terminal.integrated.env.windows": {
    "CHERE_INVOKING": "1",
    "MSYSTEM": "MINGW64",
    "MSYS2_PATH_TYPE": "inherit",
},
"terminal.integrated.shellArgs.windows": [
    "/usr/bin/zsh",
    "-l",
    "-i",
],
查看更多
Bombasti
3楼-- · 2020-02-07 17:38

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
查看更多
来,给爷笑一个
4楼-- · 2020-02-07 17:48

This works for me with Visual Studio Code version shown below

"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",
    "PATH" : "/mingw64/bin:/usr/local/bin:/usr/bin:/bin:/c/Windows/System32:/c/Windows:/c/Windows/System32/Wbem:/c/Windows/System32/WindowsPowerShell/v1.0/"         
},

VS Code version

查看更多
不美不萌又怎样
5楼-- · 2020-02-07 17:53

I got this to work

{
    "terminal.integrated.shell.windows": "C:\\msys64\\usr\\bin\\bash.exe",
    "terminal.integrated.shellArgs.windows": ["-lic", "cd $OLDPWD; exec bash"],
}
查看更多
做个烂人
6楼-- · 2020-02-07 17:56

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"
    }
}
查看更多
祖国的老花朵
7楼-- · 2020-02-07 18:00
{
    "terminal.integrated.shell.windows": "C:\\msys64\\usr\\bin\\sh.exe",
    "terminal.integrated.shellArgs.windows": ["--login", "-i"]
}

Worked for me.

查看更多
登录 后发表回答