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


当前回答

我恰好是一家财富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"
}

其他回答

最新的VS代码:

如果你看不到设置。进入菜单文件->首选项->设置(或按Ctrl+,) 设置出现,见两个选项卡用户(默认选择)和工作空间。进入“用户->功能->终端” 终端部分出现,请参见settings.json中的链接编辑。点击并添加"terminal.integrated.shell.windows": "C:\\Program Files\\Git\\bin\\bash.exe" 保存并重新启动VS代码。

Bash终端将反映在终端上。

分离的或不相关的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"

要在Visual Studio Code中打开终端,你不需要在任何地方都使用推荐的Ctrl + ',因为这是非常不方便的手指操作。更好的解决方案是:

Ctrl + j

Vs代码1.60.0

因为我正面临着前面的问题,Git Bash根本没有被识别,尽管之前已经安装了。要让上述任何解决方案发挥作用,您需要遵循本主题中讨论的说明和下面列出的说明。

转到settings.json 通过添加“git. sh”来启用git bash。启用”:真正的 并通过添加“git”来定义它的路径。path": "<你的路径到GIT>\\ GIT \\bin\\bash.exe"

之后,指令(例如由@rustyhu定义的指令)将起作用。

PS:希望我能在调试最初陈述的问题时为您节省几个小时的时间

VSCODE 1.63.2

即使把这里的答案结合起来,我也不能让它工作。也许,我有一个奇异的设置。我会得到这个错误,设置"terminal.integrated.defaultProfile.windows": "Git Bash",它仍然只会打开Powershell, Git Bash无处可见:

我的解决方法是修改命令提示符的入口,通过改变它指向bash的路径,然后将其设置为默认值,如下所示:

"terminal.integrated.profiles.windows": {
    "PowerShell": {
        "source": "PowerShell",
        "icon": "terminal-powershell"
    },
    "Command Prompt": {
        "path": [
            "C:\\Path\\To\\Git\\bin\\bash.exe",
            // "${env:windir}\\Sysnative\\cmd.exe",
            // "${env:windir}\\System32\\cmd.exe"
        ],
        "args": [],
        "icon": "terminal-cmd"
    
},
"terminal.integrated.defaultProfile.windows": "Command Prompt",