如何将MSYS2 shell集成到Window上的Visual Studio代码中?

时间:2017-08-23 10:03:41

标签: bash shell visual-studio-code msys2

我尝试将MSYS2 shell集成到Visual Studio Code集成终端中。这是我的用户设置:

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

但是,我遇到了--login将当前工作目录更改为Windows主页的问题。我希望当前目录位于我的工作区的根目录。

我的进一步尝试是尝试添加标记-c 'cd ${workspaceRoot}'。然而,bash会在开始时崩溃。我可以通过删除--login来正确访问当前目录,但是没有登录模式,所有其他shell命令(lscd等)都不可用。

如何将MSYS2 shell正确集成到我的vscode中?

10 个答案:

答案 0 :(得分:17)

要禁止从当前目录更改工作目录,请将grid-auto-flow: dense;环境变量设置为非空值:

CHERE_INVOKING

在MSYS登录脚本中,设置 "terminal.integrated.env.windows": { "CHERE_INVOKING": "1" }, 变量仅用于防止shell执行CHERE_INVOKING,而不执行任何操作。

如果需要MinGW工具链,请设置cd "${HOME}"环境变量以选择工具链。识别的值为MSYS(默认值),MINGW32或MINGW64。

MSYSTEM

完整的,VS Code设置可能如下所示:

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

提供关于{ "terminal.integrated.shell.windows": "C:\\msys64\\usr\\bin\\bash.exe", "terminal.integrated.shellArgs.windows": [ "--login", ], "terminal.integrated.env.windows": { "CHERE_INVOKING": "1", "MSYSTEM": "MINGW64", }, } 非常神秘命名的一些背景:CHERE_INVOKING显然是Cygwin command for installing and managing a "Command Prompt Here" folder context menu item.虽然MSYS2继承了Cygwin的环境变量,但实际上并没有继承命令本身。

答案 1 :(得分:9)

原创但不能100%工作(接受为答案)

这将正确启动MSYS2 bash shell,以便执行 .bash_login

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

修改

当时原来的答案似乎有效,但当我尝试在VSCode中开始使用任务时,它显然无法正常工作。尝试运行一个简单调用 make all 的任务会导致以下错误:

  

/ usr / bin / bash:/ d:没有这样的文件或目录
  终端进程以退出代码终止:127

从其他答案中,使用"terminal.integrated.shellArgs.windows": ["--login", "-i"]获得了几乎正确的环境(MSYS而不是MINGW64),但是在错误的目录中启动,并且"terminal.integrated.shellArgs.windows": ["-lic", "cd $OLDPWD; exec bash"]在正确的目录中启动了正确的环境但是可以没有运行任务。

我想出了迄今为止似乎工作正常的解决方案 在VSCode设置中:

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

.bashrc

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

答案 2 :(得分:6)

这对我有用:

use strict;
use warnings;

my %hash;
while (<DATA>) {
    chomp;
    my @F = split ',';
    my $key = $F[0];       
    for my $i (0..$#F) {
        $hash{$key}->[$i] //= $F[$i];   
    }
}

for my $key (sort keys %hash) {
    print join(',',@{$hash{$key}})."\n";
}

__DATA__
aaaa,3
aaaa,,5
aaaa,,,1
aaaa,,,,3
bb,2
bb,,4
bb,,,,,,,,,,6
cc,,,,,,5
dd,5

答案 3 :(得分:1)

我让这个工作

df = pd.DataFrame({'A': [[1,2,3],[1,3,5],[4,84,68]]},index=[0,1,0])

答案 4 :(得分:1)

此处的答案来自旧方法,该方法现在(2021 年 7 月)已在 VSCode 中弃用 新建议的方式,将其添加到 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"
      }
    }
  },

参考:integrated-terminal#_configuring-profiles

答案 5 :(得分:0)

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

为我工作。

答案 6 :(得分:0)

调用shell执行任务时,vs代码将/d /c个参数添加到shell可执行文件中,就像cmd.exe一样(参见cli中的cmd /?)。当然MSYS2 bash不会将/d /c解释为有效的参数,这就是为什么它在尝试将/d: No such file or directory解释为命令时回答/d的原因。 我刚刚开始挖掘VSCode,我感觉用户配置中缺少一些配置参数,告诉vscode调用msys2_shell.cmd(即用-c arg)执行任务的好方法(没有pb)对于集成/交互式shell,因为调用bash时没有传递arg)。 无论如何,为了解决这个问题,我们可以编辑msys2_shell.cmd来陷阱\d \c并使用-c调用bash:

@echo off
setlocal

rem usefull get what VSCode pass 
rem echo "given params %1 %2 %3 %4 %5 %6 %7 %8 %9"

set "WD=%__CD__%"
if NOT EXIST "%WD%msys-2.0.dll" set "WD=%~dp0usr\bin\"

rem To activate windows native symlinks uncomment next line
rem set MSYS=winsymlinks:nativestrict

rem Set debugging program for errors
rem set MSYS=error_start:%WD%../../mingw64/bin/qtcreator.exe^|-debug^|^<process-id^>

rem To export full current PATH from environment into MSYS2 use '-use-full-path' parameter
rem or uncomment next line
rem set MSYS2_PATH_TYPE=inherit

:checkparams
rem Help option
if "x%~1" == "x-help" (
  call :printhelp "%~nx0"
  exit /b %ERRORLEVEL%
)
if "x%~1" == "x--help" (
  call :printhelp "%~nx0"
  exit /b %ERRORLEVEL%
)
if "x%~1" == "x-?" (
  call :printhelp "%~nx0"
  exit /b %ERRORLEVEL%
)
if "x%~1" == "x/?" (
  call :printhelp "%~nx0"
  exit /b %ERRORLEVEL%
)
rem Shell types
if "x%~1" == "x-msys" shift& set MSYSTEM=MSYS& goto :checkparams
if "x%~1" == "x-msys2" shift& set MSYSTEM=MSYS& goto :checkparams
if "x%~1" == "x-mingw32" shift& set MSYSTEM=MINGW32& goto :checkparams
if "x%~1" == "x-mingw64" shift& set MSYSTEM=MINGW64& goto :checkparams
if "x%~1" == "x-mingw" shift& (if exist "%WD%..\..\mingw64" (set MSYSTEM=MINGW64) else (set MSYSTEM=MINGW32))& goto :checkparams
rem Console types
if "x%~1" == "x-mintty" shift& set MSYSCON=mintty.exe& goto :checkparams
if "x%~1" == "x-conemu" shift& set MSYSCON=conemu& goto :checkparams
if "x%~1" == "x-defterm" shift& set MSYSCON=defterm& goto :checkparams
rem Other parameters
if "x%~1" == "x-full-path" shift& set MSYS2_PATH_TYPE=inherit& goto :checkparams
if "x%~1" == "x-use-full-path" shift& set MSYS2_PATH_TYPE=inherit& goto :checkparams
if "x%~1" == "x-here" shift& set CHERE_INVOKING=enabled_from_arguments& goto :checkparams
if "x%~1" == "x-where" (
  if "x%~2" == "x" (
    echo Working directory is not specified for -where parameter. 1>&2
    exit /b 2
  )
  cd /d "%~2" || (
    echo Cannot set specified working diretory "%~2". 1>&2
    exit /b 2
  )
  set CHERE_INVOKING=enabled_from_arguments
)& shift& shift& goto :checkparams
if "x%~1" == "x-no-start" shift& set MSYS2_NOSTART=yes& goto :checkparams

rem VS CODE  CMD.EXE argument PATCH
if "x%~1" == "x/d" (
    if "x%~2" == "x/c" (
        rem we got a vs code task issued
        echo "VSCODE_TASK detected"
        shift& shift
        set VSCODE_TASK=yes 
        goto :checkparams
    )
)& shift& goto :checkparams


rem Setup proper title
if "%MSYSTEM%" == "MINGW32" (
  set "CONTITLE=MinGW x32"
) else if "%MSYSTEM%" == "MINGW64" (
  set "CONTITLE=MinGW x64"
) else (
  set "CONTITLE=MSYS2 MSYS"
)

if "x%MSYSCON%" == "xmintty.exe" goto startmintty
if "x%MSYSCON%" == "xconemu" goto startconemu
if "x%MSYSCON%" == "xdefterm" goto startsh

if NOT EXIST "%WD%mintty.exe" goto startsh
set MSYSCON=mintty.exe
:startmintty
if not defined MSYS2_NOSTART (
  start "%CONTITLE%" "%WD%mintty" -i /msys2.ico -t "%CONTITLE%" /usr/bin/bash --login %1 %2 %3 %4 %5 %6 %7 %8 %9
) else (
  "%WD%mintty" -i /msys2.ico -t "%CONTITLE%" /usr/bin/bash --login %1 %2 %3 %4 %5 %6 %7 %8 %9
)
exit /b %ERRORLEVEL%

:startconemu
call :conemudetect || (
  echo ConEmu not found. Exiting. 1>&2
  exit /b 1
)
if not defined MSYS2_NOSTART (
  start "%CONTITLE%" "%ComEmuCommand%" /Here /Icon "%WD%..\..\msys2.ico" /cmd "%WD%bash" --login %1 %2 %3 %4 %5 %6 %7 %8 %9
) else (

  "%ComEmuCommand%" /Here /Icon "%WD%..\..\msys2.ico" /cmd "%WD%bash" --login %1 %2 %3 %4 %5 %6 %7 %8 %9
)
exit /b %ERRORLEVEL%

:startsh
set MSYSCON=
if not defined MSYS2_NOSTART (
  start "%CONTITLE%" "%WD%bash" --login %1 %2 %3 %4 %5 %6 %7 %8 %9
) else (
  rem integrated shell called (interactive)
  if not defined VSCODE_TASK (
    "%WD%bash" --login %1 %2 %3 %4 %5 %6 %7 %8 %9
  ) else (
    rem Call bash with -c arg (execute the command in argument and quit)
    rem echo  "start  %WD%bash" --login -c "%1 %2 %3 %4 %5 %6 %7 %8 %9"
    "%WD%bash" --login -c "%1 %2 %3 %4 %5 %6 %7 %8 %9"
  )
)
exit /b %ERRORLEVEL%

:EOF
exit /b 0

:conemudetect
set ComEmuCommand=
if defined ConEmuDir (
  if exist "%ConEmuDir%\ConEmu64.exe" (
    set "ComEmuCommand=%ConEmuDir%\ConEmu64.exe"
    set MSYSCON=conemu64.exe
  ) else if exist "%ConEmuDir%\ConEmu.exe" (
    set "ComEmuCommand=%ConEmuDir%\ConEmu.exe"
    set MSYSCON=conemu.exe
  )
)
if not defined ComEmuCommand (
  ConEmu64.exe /Exit 2>nul && (
    set ComEmuCommand=ConEmu64.exe
    set MSYSCON=conemu64.exe
  ) || (
    ConEmu.exe /Exit 2>nul && (
      set ComEmuCommand=ConEmu.exe
      set MSYSCON=conemu.exe
    )
  )
)
if not defined ComEmuCommand (
  FOR /F "tokens=*" %%A IN ('reg.exe QUERY "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\ConEmu64.exe" /ve 2^>nul ^| find "REG_SZ"') DO (
    set "ComEmuCommand=%%A"
  )
  if defined ComEmuCommand (
    call set "ComEmuCommand=%%ComEmuCommand:*REG_SZ    =%%"
    set MSYSCON=conemu64.exe
  ) else (
    FOR /F "tokens=*" %%A IN ('reg.exe QUERY "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\ConEmu.exe" /ve 2^>nul ^| find "REG_SZ"') DO (
      set "ComEmuCommand=%%A"
    )
    if defined ComEmuCommand (
      call set "ComEmuCommand=%%ComEmuCommand:*REG_SZ    =%%"
      set MSYSCON=conemu.exe
    )
  )
)
if not defined ComEmuCommand exit /b 2
exit /b 0

:printhelp
echo Usage:
echo     %~1 [options] [bash parameters]
echo.
echo Options:
echo     -mingw32 ^| -mingw64 ^| -msys[2]   Set shell type
echo     -defterm ^| -mintty ^| -conemu     Set terminal type
echo     -here                            Use current directory as working
echo                                      directory
echo     -where DIRECTORY                 Use specified DIRECTORY as working
echo                                      directory
echo     -[use-]full-path                 Use full currnent PATH variable
echo                                      instead of triming to minimal
echo     -no-start                        Do not use "start" command and
echo                                      return bash resulting errorcode as
echo                                      this batch file resulting errorcode
echo     -help ^| --help ^| -? ^| /?         Display this help and exit
echo.
echo Any parameter that cannot be treated as valid option and all
echo following parameters are passed as bash command parameters.
echo.
exit /b 0

答案 7 :(得分:0)

accepted answer对我有用,但是使用zsh shell("terminal.integrated.shell.windows": "C:\\msys64\\usr\\bin\\zsh.exe")时,事情要比使用bash慢得多。我不知道为什么,但是使用ConEmu's cygwin/msys terminal connector有很大帮助:

"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",
],

答案 8 :(得分:0)

这对我来说适用于下面显示的Visual Studio Code版本

"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

答案 9 :(得分:0)

按F1 +编写settings.json 然后,在{}的末尾添加以下几行:

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

它看起来像这样:(显然,settings.json与下一个示例可能不同)

"telemetry.enableTelemetry": true,
"code-runner.saveFileBeforeRun": true,
"code-runner.saveAllFilesBeforeRun": true,
"code-runner.runInTerminal": true,
"liveshare.audio.startCallOnShare": true,
"window.zoomLevel": 1,
"explorer.confirmDelete": false,
"C_Cpp.updateChannel": "Insiders",

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

请记住,在粘贴建议的行之前,您必须先添加逗号。