Windows上的Visual Studio Code默认使用PowerShell作为集成终端。如果你想从Visual Studio Code中使用Bash,应该遵循哪些步骤?


当前回答

在2022年7月,我通过在花括号内添加以下内容来解决这个问题。

"terminal.integrated.defaultProfile.windows": "Git Bash"

注意Git和Bash之间的空白。

我的设置。Json是这样的:

{
    "python.defaultInterpreterPath": "C:\\Users\\Prakh\\AppData\\Local\\Programs\\Python\\Python39\\python.exe",
    "python.diagnostics.sourceMapsEnabled": true,
    "python.linting.pylintEnabled": true,
    "python.linting.enabled": true,
    "code-runner.runInTerminal": true,
    "terminal.integrated.defaultProfile.windows": "Git Bash"
}

现在重新启动VS code并关闭所有现有终端并打开一个新终端。

其他回答

更新:较新版本的Visual Studio Code在终端下拉菜单中有选择默认Shell命令:

记住,它只是列出了%PATH%环境变量中的shell。对于不在路径上的shell,请参阅其他答案。

额外提示:当你启动bash时,它只会执行.bashrc,如果你在.bash_profile中有初始化命令,你必须将它复制到.bashrc。在Git Bash中使用Conda环境是必不可少的。

1.36版本之前(2019年6月)

现在最简单的方法(至少从Visual Studio Code 1.22开始)是按Shift + Ctrl + P打开命令面板,然后输入:

Select Default Shell

现在你可以很容易地在路径中选择你喜欢的shell:

对于不在%PATH%中的shell,请参阅其他答案。

参见完整的Visual Studio Code shell参考。有很多肉的东西。

这取决于你是只在当前用户还是所有用户中安装了Git Bash:

如果所有用户都安装了,那么在用户设置中输入"terminal.integrated.shell.windows": "C:\\Program Files\\Git\\bin\\bash.exe" (Ctrl +逗号)。

如果它只安装在当前用户上,则在用户设置(Ctrl +逗号)中输入“terminal.integrated.shell.windows”:“C:\\Users\\<您的用户>\\AppData\\Local\\Programs\\Git\\bin\\bash.exe”。

如果上面列出的方法不起作用,那么你应该尝试克里斯特的解决方案,它说-

如果你想要集成环境,你需要指向Git安装的bin文件夹中的sh.exe文件。 因此,配置应该是C:\\<my-git-install>\\bin\\sh.exe。

注意:sh.exe和bash.exe在我看来完全一样。它们之间应该没有区别。

由于Visual Studio Code的最新更新,事情发生了一些变化。下面的步骤对我有用。

按Ctrl + Shift + P打开Visual Studio Code命令菜单。 在文本区输入>preferences: Open Settings (JSON)。 在JSON文件的末尾添加以下行 右边窗格。 "terminal.integrated.shell.windows": "C:\\Program Files\\Git\\bin\\bash.exe" 关闭并重新打开Visual Studio Code实例。

分离的或不相关的shell和代码[参数]支持呢?

虽然其他答案讨论了如何配置和使用VScode集成的WSL bash终端支持,但他们并没有解决“分离外壳”的问题:外壳不是从VScode内部启动的,或者以某种方式从与IDE相关的VScode服务器实例中“断开连接”。

这样的shell会给出如下错误:

命令只能在WSL或Visual Studio Code终端中使用。

还是……

无法连接到VS Code服务器。 请求出错

下面是一个脚本,它可以很容易地解决这个问题。

我每天都使用这个工具将tmux会话中的shell与特定的VScode服务器实例连接起来,或者修复从其宿主IDE分离的集成shell。

#!/bin/bash
# codesrv-connect
#
#  Purpose:
#     Copies the vscode connection environment from one shell to another, so that you can use the
#     vscode integrated terminal's "code [args]" command to communicate with that instance of vscode
#     from an unrelated shell.
#
#  Usage:
#    1.  Open an integrated terminal in vscode, and run codesrv-connect
#    2.  In the target shell, cd to the same directory and run
#       ". .codesrv-connect", or follow the instruction printed by codesrv-connect.
#
#  Setup:
#    Put "codesrv-connect somewhere on your PATH (e.g. ~/bin)"
#
#  Cleanup:
#    - Delete abandoned .codesrv-connect files when their vscode sessions die.
#    - Do not add .codesrv-connect files to git repositories.
#
#  Notes:
#     The VSCODE_IPC_HOOK_CLI environment variable points to a socket which is rather volatile, while the long path for the 'code' alias is more stable: vscode doesn't change the latter even across a "code -r ." reload.  But the former is easily detached and so you need a fresh value if that happens.  This is what codesrv-connect does: it captures the value of these two and writes them to .codesrv-connect in the current dir.
#
#   Verinfo: v1.0.0 - les.matheson@gmail.com - 2020-03-31
#

function errExit {
    echo "ERROR: $@" >&2
    exit 1
}

[[ -S $VSCODE_IPC_HOOK_CLI ]] || errExit "VSCODE_IPC_HOOK_CLI not defined or not a pipe [$VSCODE_IPC_HOOK_CLI]"
if [[ $(which code) != *vscode-server* ]]; then
    errExit "The 'code' command doesn't refer to something under .vscode-server: $(type -a code)"
fi
cat <<EOF >.codesrv-connect
# Temp file created by $(which codesrv-connect): source this into your working shell like '. .codesrv-connect'
# ( git hint: add ".codesrv-connect" to .gitignore )
#
cd "$PWD"
if ! test -S "$VSCODE_IPC_HOOK_CLI"; then
    echo "ERROR: $VSCODE_IPC_HOOK_CLI not a socket. Dead session."
else
    export VSCODE_IPC_HOOK_CLI="$VSCODE_IPC_HOOK_CLI"
    alias code=$(which code)
    echo "Done: the 'code' command will talk to socket \"$VSCODE_IPC_HOOK_CLI\" now."
    echo "You can delete .codesrv-connect when the vscode server context dies, or reuse it in other shells until then."
fi
EOF

echo "# OK: run this to connect to vscode server in a destination shell:"
echo ". $PWD/.codesrv-connect"

我恰好是一家财富500强公司的顾问,不幸的是,我用的是Windows 7,没有管理员权限。因此,Node.js, Npm, Visual Studio Code等。被推到我的机器-我不能改变很多,等等…

对于这台运行Windows 7的电脑:

下面是我的新设置。不工作的将被注释掉。

{
    "update.channel": "none",
    "terminal.integrated.shell.windows": "C:\\Program Files\\Git\\bin\\bash.exe"
    //"terminal.integrated.shell.windows": "C:\\Windows\\sysnative\\bash.exe"
}