bash 如何将 MSYS2 shell 集成到 Window 上的 Visual Studio 代码中?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45836650/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I integrate MSYS2 shell into Visual studio code on Window?
提问by pbeta
I tried to integrate MSYS2 shell into Visual studio Code integrated terminal. Here's my user settings:
我尝试将 MSYS2 shell 集成到 Visual Studio Code 集成终端中。这是我的用户设置:
{
"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.
但是,我遇到了--login
将当前工作目录更改为 Windows home 的问题。我希望当前目录位于我的工作区的根目录下。
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.
我的进一步尝试是尝试添加标志-c 'cd ${workspaceRoot}'
。但是,bash 会在启动时崩溃。我可以正确得到当前目录中去除--login
,但没有登录模式,所有其他的shell命令(ls
,cd
,等)不可用。
How do I properly integrate MSYS2 shell into my vscode?
如何将 MSYS2 shell 正确集成到我的 vscode 中?
回答by Jānis Rūcis
To inhibit the working directory change from your current directory, set the CHERE_INVOKING
environment variable to a non-empty value:
要禁止从当前目录更改工作目录,请将CHERE_INVOKING
环境变量设置为非空值:
"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.
在 MSYS 登录脚本中,设置CHERE_INVOKING
变量仅用于防止 shell 执行 a cd "${HOME}"
,而没有其他作用。
If you require a MinGW toolchain, set the MSYSTEM
environment variable to select a toolchain. Recognized values are MSYS (default), MINGW32 or MINGW64.
如果您需要 MinGW 工具链,请设置MSYSTEM
环境变量以选择工具链。可识别的值为 MSYS(默认)、MINGW32 或 MINGW64。
"terminal.integrated.env.windows": {
"MSYSTEM": "MINGW64",
},
In full, the VS Code settings might look like so:
完整的 VS Code 设置可能如下所示:
{
"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.
要提供的很神秘命名一些背景CHERE_INVOKING
:chere
显然是一个用于安装和管理“命令提示符这里的”文件夹右键菜单项Cygwin的命令。尽管 MSYS2 从 Cygwin 继承了环境变量,但它实际上并没有继承命令本身。
回答by cola
Original but not working 100% (accepted as the answer)
原始但不工作 100%(被接受为答案)
This will start the MSYS2 bash shell properly so your .bash_logingets executed:
这将正确启动 MSYS2 bash shell,以便您的.bash_login被执行:
"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 allcaused the following error:
最初的答案当时似乎有效,但是当我尝试在 VSCode 中开始使用任务时,它显然不起作用。尝试运行一个简单地调用make all的任务会导致以下错误:
/usr/bin/bash: /d: No such file or directory
The terminal process terminated with exit code: 127
/usr/bin/bash: /d: No such file or directory
终端进程以退出代码终止: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.
从其他答案来看, using"terminal.integrated.shellArgs.windows": ["--login", "-i"]
获得了几乎正确的环境(MSYS 而不是 MINGW64),但在错误的目录中"terminal.integrated.shellArgs.windows": ["-lic", "cd $OLDPWD; exec bash"]
启动,并在具有正确环境的正确目录中启动,但无法运行任务。
I came up with this solution that so far seems to work fine.
In VSCode settings:
我想出了这个到目前为止似乎工作正常的解决方案。
在 VSCode 设置中:
"terminal.integrated.shell.windows": "C:\msys64\usr\bin\bash.exe",
"terminal.integrated.env.windows":
{
"MSYSTEM": "MINGW64",
//"MSYS2_PATH_TYPE": "inherit",
"MSVSCODE": "1"
},
In .bashrc:
在.bashrc 中:
if [ ! -z "$MSVSCODE" ]; then
unset MSVSCODE
source /etc/profile
cd $OLDPWD
fi
回答by Rodrigo Hernandez
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"
}
}
回答by user2241515
I got this to work
我让这个工作
{
"terminal.integrated.shell.windows": "C:\msys64\usr\bin\bash.exe",
"terminal.integrated.shellArgs.windows": ["-lic", "cd $OLDPWD; exec bash"],
}
回答by Borek Bernard
The accepted answerworks for me but with zsh shell ("terminal.integrated.shell.windows": "C:\\msys64\\usr\\bin\\zsh.exe"
), things are muchslower than with bash. I don't know why but using ConEmu's cygwin/msys terminal connectorgreatly helped:
该接受的答案对我的作品,但与zsh的外壳("terminal.integrated.shell.windows": "C:\\msys64\\usr\\bin\\zsh.exe"
),事情远不是在bash慢。我不知道为什么,但使用ConEmu 的 cygwin/msys 终端连接器有很大帮助:
"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",
],
回答by Sy Tran
This works for me with Visual Studio Code version shown below
这适用于我的 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/"
},
回答by Dje53
When calling the shell to execute task, vs code add /d /c
arguments to the shell executable as it would for cmd.exe
(see cmd /?
in cli). Of course MSYS2 bash don't interpret /d /c
as valid params, that s why it answers /d: No such file or directory
as it try to interpret /d
as a command.
I ve just began to dig in VSCode and I have the feeling some config parameter is missing in user configuration to tell vscode the good way to call msys2_shell.cmd
(ie with -c
arg) for task execution (no pb for integrated/interactive shell as no arg is passed when bash is invoked).
Anyway, to overcome the problem it is possible to edit msys2_shell.cmd
to trap \d \c
and call bash with -c
:
当调用 shell 执行任务时,vs 代码将/d /c
参数添加到 shell 可执行文件中cmd.exe
(参见cmd /?
cli)。当然,MSYS2 bash 不会解释/d /c
为有效的参数,这就是它/d: No such file or directory
在尝试解释/d
为命令时回答的原因。我刚刚开始深入研究 VSCode,我感觉用户配置中缺少一些配置参数来告诉 vscode 调用msys2_shell.cmd
(即使用-c
arg)进行任务执行的好方法(集成/交互式 shell 没有 pb,因为没有传递 arg当 bash 被调用时)。无论如何,为了克服这个问题,可以编辑msys2_shell.cmd
陷阱\d \c
并使用以下命令调用 bash -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
回答by n370
{
"terminal.integrated.shell.windows": "C:\msys64\usr\bin\sh.exe",
"terminal.integrated.shellArgs.windows": ["--login", "-i"]
}
Worked for me.
对我来说有效。