2017-08-23 507 views
4

我試圖將MSYS2 shell集成到Visual Studio Code集成終端中。這裏是我的用戶設置:如何將MSYS2 shell集成到Window上的Visual Studio代碼中?

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

不過,我跑到哪裏--login改變當前工作目錄到Windows家庭問題。我想讓當前目錄位於我的工作區的根目錄下。

我的進一步嘗試是我嘗試添加一個標誌-c 'cd ${workspaceRoot}'。但是,bash會在開始時崩潰。我可以通過刪除--login來正確進入當前目錄,但是如果沒有登錄模式,則所有其他shell命令(ls,cd等)都不可用。

如何正確地將MSYS2 shell集成到我的vscode中?

回答

2

原來的,但不工作100%(接受答案)

這將正常啓動MSYS2的bash shell:

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

編輯

原來的答案似乎在工作,但當我試圖開始使用VSCode中的任務時,它顯然無法正常工作。試圖運行一個簡單地稱爲讓所有引起以下錯誤任務:

/usr/bin/bash: /d: No such file or directory
The terminal process terminated with exit code: 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 
+0

我通常開始msys2使用' - 使用 - 全path'參數,所以我的PATH變量保持不變。 –

1

我讓你.bash_login文件被執行這項工作

{ 
    "terminal.integrated.shell.windows": "C:\\msys64\\usr\\bin\\bash.exe", 
    "terminal.integrated.shellArgs.windows": ["-lic", "cd $OLDPWD; exec bash"], 
} 
0
{ 
    "terminal.integrated.shell.windows": "C:\\msys64\\usr\\bin\\sh.exe", 
    "terminal.integrated.shellArgs.windows": ["--login", "-i"] 
} 

爲我工作。

1

這個工作對我來說:

{ 
    "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" 
    } 
} 
6

禁止從當前目錄的工作目錄的變化,CHERE_INVOKING環境變量設置爲一個非空值:

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

在MSYS登錄腳本,設置CHERE_INVOKING變量僅用於防止shell執行cd "${HOME}",而沒有其他操作。

如果您需要MinGW工具鏈,請設置MSYSTEM環境變量以選擇工具鏈。識別的值是MSYS(默認),MINGW32或MINGW64。

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

在充滿後,VS代碼設置看起來可能是這樣:

{ 
    "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非常神祕的命名一些背景:chere顯然是一個Cygwin command for installing and managing a "Command Prompt Here" folder context menu item.雖然MSYS2繼承來自Cygwin的環境變量,它實際上並不繼承命令本身。

+0

這實際上對我來說真的很好。謝謝。 – realhu

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)執行任務的好方法(對於集成/交互式shell,沒有pb爲no arg在調用bash時傳遞)。 無論如何,要克服這個問題,可以編輯msys2_shell.cmd陷阱\d \c並調用慶典與-c

@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 
相關問題