ChatGPT解决这个技术问题 Extra ChatGPT

如何从 Visual Studio Code 集成终端在 Windows 上使用 Bash?

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

请阅读 stackoverflow.com/questions/43427631/… 以回答您的问题。

j
jDelforge

从 https://git-scm.com/download/win 安装 Git 打开 Visual Studio Code 并按住 Ctrl + ` 打开终端。使用 Ctrl + Shift + P 打开命令面板。 类型 - 选择默认配置文件 从选项中选择 Git Bash 单击终端窗口中的 + 图标 新终端现在将成为 Git Bash 终端。给它几秒钟来加载 Git Bash 现在您也可以从终端的下拉菜单中切换不同的终端。


这对我有用。我通过手动添加默认 bash 尝试了所有其他指南,但它每次都打开一个新的终端窗口。非常感谢
这就是我正在寻找的......这应该是更简单的接受答案
完美解释..谢谢你的回复。
更简单、更快捷的解决方案
请注意,这现在显示为“选择默认配置文件”
F
FruityOatyBar

您不再需要手动输入 bash.exe 路径。此答案已弃用。如果您在默认路径中安装了 git,现在您可以直接切换到 bash。如果您将 git 安装到不同的路径,则需要使用以下解决方案。

https://git-scm.com/download/win 安装 Git。

然后打开 Visual Studio Code 并使用 Ctrl + Shift + P 打开命令面板。然后键入“打开用户设置”,然后从下拉菜单中选择“打开用户设置”。

https://i.stack.imgur.com/uBqiA.png

然后此选项卡将打开,左侧为默认设置,右侧为您的设置:

https://i.stack.imgur.com/LxSyI.png

现在将这行代码复制到您自己的设置页面(右侧窗格)并保存 - "terminal.integrated.shell.windows": "C:\\Program Files\\Git\\bin\\bash.exe"

注意: "C:\\Program Files\Git\bin\bash.exe" 是 Git 安装中 bash.exe 文件所在的路径。如果您使用的是适用于 Linux 的 Windows 子系统 (WSL) Bash shell,则路径为 "C:\Windows\System32\bash.exe"

现在按 Ctrl + ` 从 Visual Studio Code 打开终端。你将拥有 Bash -

https://i.stack.imgur.com/DN7sV.png


两个可能有用的信息:确保在编写路径时转义 json 中的反斜杠字符。还要确保在 vscode 中包含 64 位版本的 git bash,因为默认情况下 32 位版本可能没有颜色。 (只需从路径中删除“(x86)”部分)
添加到 Lajos,如果您仍然无法看到终端打开,请尝试重新启动 vscode。
我遇到了 "terminal.integrated.shell.windows": "C:\\Program Files\\Git\bin\bash.exe" 的问题,终端没有打开。试过 "terminal.integrated.shell.windows": "C:\\Program Files\\Git\\bin\\bash.exe" 。非常适合我。
仅供参考,请确保您链接到 bash.exe,而不是 git-bash.exe。后者将在 VS 代码外部打开一个终端作为单独的窗口,而 bash.exe 将在 VS 代码内部运行。
如何在用户设置中找到 settings.json,这里没有提到,我不得不用 VSCode 玩一点才能找到它。我们需要在“搜索设置”文本框中输入“终端”,你会看到一个链接“在settings.json中编辑”,你需要点击这里。
n
neves

更新:较新版本的 Visual Studio Code 在终端下拉菜单中有 Select Default Shell 命令:

https://i.stack.imgur.com/JhS4a.png

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

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

1.36 版之前(2019 年 6 月)

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

Select Default Shell

现在,您可以轻松地在路径中找到的 shell 之间选择您喜欢的 shell:

https://i.stack.imgur.com/4GaC9.png

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

请参阅the complete Visual Studio Code shell reference。有很多肉的东西。


这就是我一直在寻找的答案。我想使用 WSL Bash 但其他答案是尝试使用 git 附带的 bash
有没有办法打开一个特定的外壳......而不是让它成为“默认”?我认为这一次有一个“开放的'bash'”是有意义的。
I
Isaac Sekamatte

按住 Ctrl + ` 打开终端。在终端中,键入 bash 以在终端中使用 Git Bash。注意:确保您的机器上安装了 Git Bash。

如果您想再次使用 PowerShell,只需在终端中键入 powershell。要使用 Windows 命令行,请在终端中键入 cmd

您选择的设置将用作您的默认设置。


这为所选答案增加了更多价值
要“切换回 powershell”,您可能需要从在默认 shell 中运行的 bash shell 执行 exit,然后键入 powershell,因为 bash 不知道 powershell 是什么。
D
Dilip Muthukurussimana

对我来说,这是唯一有效的组合!

"terminal.integrated.shell.windows": "C:\\Program Files\\Git\\git-cmd.exe",
"terminal.integrated.shellArgs.windows": [
  "--command=usr/bin/bash.exe",
  "-l",
  "-i"
]

使用 git-bash.exe 作为 ...shell.windows,每次 bash 在 VS 之外打开!

感谢上帝,它终于奏效了!!否则,我打算完全清除 VS 并重新安装它(让我重新安装所有扩展并重做我的自定义!)


这真的帮助了我。我按照上面之前的答案让 bash 工作,但是我丢失了所有的 bash 和 git 别名。这把他们买回来了。谢谢。
这对我有用: "terminal.integrated.shell.windows": "C:\\Program Files\\Git\\bin\\bash.exe", "terminal.integrated.shellArgs.windows": [ "- -登录”,“-i”]
这个解决方案也对我有用。需要注意的是,在我的情况下,为了减少 gitbash 的滞后,请按照这篇文章 stackoverflow.com/questions/32232978/… 并在 Windows 中设置一个新的 HOME 环境变量。为了让它在 VSCode 中正常工作,上面的答案非常有效。
P
Peter Mortensen

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

按 Ctrl + Shift + P 打开 Visual Studio Code 命令面板。键入 >preferences: 在文本区域中打开设置 (JSON)。在右侧窗格中显示的 JSON 文件末尾添加以下行。 "terminal.integrated.shell.windows": "C:\\Program Files\\Git\\bin\\bash.exe" 关闭并重新打开您的 Visual Studio Code 实例。


r
rustyhu

正如许多其他答案已经描述的那样,Visual Studio Code 可以在配置 Terminal: Select Default Profile 的菜单中检测并列出已安装的 Git Bash,但这从未发生在我身上。对于那些像我一样不那么幸运的人,您可以手动将自定义配置文件添加到 Visual Studio Code 的 settings.json 中:

{
    // Tested in Visual Studio Code version 1.58.2, 1.59.1
    // Notice: my git install path is `D:\Git\bin\bash.exe`

    //"terminal.integrated.shell.windows": "D:\\Git\\bin\\bash.exe",
    // This works fine for me for a long time,
    // but in latest versions this is reported as deprecated,
    // you can keep this and sometimes Visual Studio Code will prompt to help
    // `migrate` it into new setting.

    // This part can be generated by Visual Studio Code
    "terminal.integrated.profiles.windows": {
        // This seems to be a reserved profile name, and also does not work for
        // me
        "Git Bash": {
            "path": "D:\\Git\\bin\\bash.exe",
            "icon": "terminal-bash"
        },
        "PowerShell": {
            "source": "PowerShell",
            "icon": "terminal-powershell"
        },
        "Command Prompt": {
            "path": [
                "${env:windir}\\Sysnative\\cmd.exe",
                "${env:windir}\\System32\\cmd.exe"
            ],
            "args": [],
            "icon": "terminal-cmd"
        },

        // Add your custom one with a different profile name from "Git Bash"
        "gitbash": {
            "path": "D:\\Git\\bin\\bash.exe",
            "icon": "terminal-bash"
        }
    },
    // Set the custom profile as default
    "terminal.integrated.defaultProfile.windows": "gitbash",

    // ...
}


对于 "terminal.integrated.defaultProfile.windows": "gitbash"- 不接受值。有效值:“PowerShell”、“命令提示符”、“Git Bash”、“JavaScript 调试终端”。
@Raul这只是一个警告,您可以尝试重新加载VS Code窗口以检查效果。如果您为 "terminal.integrated.defaultProfile.windows" 设置的配置文件名称对应于 "terminal.integrated.profiles.windows" 中添加的配置文件名称,它应该可以工作。
我的有以下行,它适用于我的系统:"terminal.integrated.defaultProfile.windows": "Git Bash"
@SherylHohman 是的,如果“Git Bash”配置文件已经存在并且可以正常工作,但如果不存在,您还可以添加自定义配置文件作为我的回答所介绍的内容。
要点是您提供的名称必须与现有(自定义)配置文件名称匹配。它可以任意命名,但两个位置的名称必须相同……这可能是@Raul 错误的来源。相应地编辑 settings.json 文件。
P
Peter Mortensen

我按照 Paul DeCarlo 的 this tutorial 使用 Windows Subsystem for Linux (WSL) 中的 Bash,而不是 Git Bash for Windows 附带的。它们与答案中的上述步骤相同,但在您的用户设置中使用以下步骤。

"terminal.integrated.shell.windows": "C:\\Windows\\sysnative\\bash.exe",

这对我来说是第一次......这对于这些东西来说是罕见的。


确实很少见。前沿
P
Peter Mortensen

至少对我而言,这将使 Visual Studio Code 打开一个新的 Bash 窗口作为外部终端。

如果您需要集成环境,您需要指向 Git 安装的 bin 文件夹中的 sh.exe 文件。

所以配置应该是C:\\<my-git-install>\\bin\\sh.exe


C
Community

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

如果它安装在所有用户上,则将 "terminal.integrated.shell.windows": "C:\\Program Files\\Git\\bin\\bash.exe" 放入您的用户设置 (Ctrl + Comma)。

如果它仅安装在当前用户上,则将 "terminal.integrated.shell.windows": "C:\\Users\\<name of your user>\\AppData\\Local\\Programs\\Git\\bin\\bash.exe" 放入您的用户设置 (Ctrl + Comma)。

如果上面列出的方法不起作用,那么您应该尝试 Christer's solution,它说 -

如果你想要集成环境,你需要指向 Git 安装的 bin 文件夹中的 sh.exe 文件。所以配置应该是 C:\\\\bin\\sh.exe。

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


这对我有用,因为我(出于某种原因)仅为当前用户安装了 git,但我通过以下方式访问了 json 设置:CTRL-SHFT-P 然后选择“首选项:打开设置(JSON)”感谢代码片段!
t
therobinkim

这个答案与投票最多的答案类似,但有一个重要区别:之前关于这个问题的很多答案都集中在运行 Git Bash,而我的答案集中在运行 WSL Bash。

在 Windows 10 计算机上启用适用于 Linux 的 Windows 子系统。打开 Visual Studio Code 并按住 Ctrl + ` 打开终端。使用 Ctrl + Shift + P 打开命令面板。类型 - 选择默认外壳。从选项中选择 WSL Bash(不是 Git Bash)。

https://i.stack.imgur.com/ECCAy.png

单击终端窗口中的 + 图标。新终端现在将是 WSL Bash 终端!


T
Teuk1

最新的 VS 代码:

如果看不到 settings.json,请转到菜单文件 -> 首选项 -> 设置(或按 Ctrl+,)

设置出现,看到两个选项卡用户(默认选择)和工作区。转到用户 -> 功能 -> 终端

出现终端部分,请参阅 settings.json 中的链接编辑。单击并添加“terminal.integrated.shell.windows”:“C:\\Program Files\\Git\\bin\\bash.exe”,

保存并重新启动 VS 代码。

Bash终端将反映在终端上。


J
Jakub

(2021,VSC v.1.55.1)

如何将 Git Bash 添加为默认 Terminal,对于那些将 Git Bash 安装在默认路径中的人:

在 Visual Studio Code 中使用 Ctrl + 打开设置,a)在搜索设置(屏幕截图上的红色框)字段类型集成自动化 b)或只需单击功能 -> 终端(ss 上的蓝色框)单击 settings.json 中的任何编辑输入您的bash.exe 位置进入“terminal.integrated.shell.windows”:“”字段

注意 1: 因为它是 JSON 文件,请记住在您的路径中使用双 \\ 而不是 \

注意 2: 不要将 bash.exe(它位于 bin 文件夹中)与 git-bash.exe 混淆,第一种情况 bash 终端将留在 VSC 中,第二种情况将在外部打开。

https://i.stack.imgur.com/2pU51.png


S
S.Dave

我的 VS 代码版本:1.56.1(代码 --version)

配置集成终端的用户设置:

Ctrl + Shift + P

类型:用户

选择:首选项:打开用户设置

点击:打开设置(JSON)按钮(靠近右上角)

设置.json:

{
    "terminal.integrated.tabs.enabled": true,
    "terminal.integrated.shell.windows": "<your installation path>\\Git\\bin\\bash.exe",
    "terminal.integrated.defaultProfile.windows": "Git Bash",
    "terminal.integrated.profiles.windows": {
        "Git Bash": {
            "path": "<your installation path>\\Git\\bin\\bash.exe",
            "icon": "terminal-bash"
        },
        "Command Prompt": {
            "path": "${env:windir}\\System32\\cmd.exe",
            "icon": "terminal-cmd"
        },
        "Windows PowerShell": {
            "path": "C:\\WINDOWS\\System32\\WindowsPowerShell\\v1.0\\powershell.exe",
            "icon": "terminal-powershell"
        }
    }
}

terminal.integrated.defaultProfile.windows

只有当您点击“添加新终端”(“+”)按钮时,此属性才会将 GitBash 设置为默认值。让它在启动时默认是不够的。

terminal.integrated.shell.windows

将显示贬低的警告。但是需要此配置以使所选外壳(此处为 GitBash)在启动时默认。

问题过滤

在问题选项卡上,在输入字段旁边,单击过滤器图标,我选中了“仅显示活动文件”选项,以便在处理其他任何内容时使这个已弃用的错误消失。


配置文件名称中不再允许有空格 - see here
P
Peter Mortensen

Git\bin 目录添加到 Path 环境变量。该目录默认为 %ProgramFiles%\Git\bin。通过这种方式,您可以在每个终端(包括 Visual Studio Code 的集成终端)中通过简单的键入 bash 访问 Git Bash。

How to set the path and environment variables in Windows


P
Peter Mortensen

我已经在 WSL 上设置了很多 conda 环境(Windows 上的 Ubuntu 上的 Bash),所以我想在 Visual Studio Code 上使用相同的 Bash 安装。

为此,我只需在 Visual Studio Code 的设置中指定此特定 Bash 可执行文件的路径(而不是 Git-Bash):

"terminal.integrated.shell.windows": "C:\\Windows\\System32\\bash.exe"

PS:要确保 Ubuntu on Bash 可执行文件安装在 Windows 机器上的位置,请打开 Command prompt(搜索:cmd)并运行:

where bash.exe


Z
Zoe stands with Ukraine

对于独家新闻用户:

"terminal.integrated.shell.windows": "C:\\Users\\[YOUR-NAME]\\scoop\\apps\\git\\current\\usr\\bin\\bash.exe",
"terminal.integrated.shellArgs.windows": [
  "-l",
  "-i"
],

S
Stabledog

分离或无关的 shell 和 code [args] 支持怎么样?

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

这样的 shell 可能会出现如下错误:

Command is only available in WSL or inside a Visual Studio Code terminal.

或者...

Unable to connect to VS Code server. Error in request

Here's a script 可以轻松解决此问题。

我每天都使用它来将 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"


对于那里的任何鱼用户,我将基于此的一些东西编译成一个小鱼插件github.com/chvolkmann/code-connect-fish-plugin
N
Ndrslmpk

VS 代码 1.60.0

当我遇到前面的问题时,Git Bash 根本没有被识别,尽管之前已经安装过。要让上述任何解决方案发挥作用,您需要遵循此 thread 中讨论并在下面列出的说明。

转到您的 settings.json 通过添加 "git.enabled": true 启用 git bash 并通过添加 "git.path": "\\Git\\bin\\bash.exe" 定义它的路径

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

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


不是 <YOUR PATH TO GIT>\\Git\\bin\\git.exe 而不是 bash.exe 吗?它用于 "git.path",git 可执行文件的路径和文件名。
"git.enabled": true,救了我的命!
n
naimur978

只需转到 Visual Studio 代码中的 settings.json 并添加以下行:

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

P
Peter Mortensen

我碰巧正在为一家 Fortune 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"
}

C
Coffee_fan

从 vscode 1.42.1 开始,我尝试了上述答案,他们的工作是为了给我一个 git bash 终端。因此,最重要的是,此设置仅适用于从终端打开 bash shell:

"terminal.integrated.shell.windows": "C:\\Program Files\\Git\\bin\\bash.exe"

然而,它也有不想要的副作用,因为它也是用于构建事物的 shell,并且会破坏 MS C++ 链,因为 bash 将用于路径分隔符的 \ 字符理解为转义字符。然后,我的完整修复需要我添加这个额外的变量,将其设置为 powershell:

"terminal.integrated.automationShell.windows": "C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe"

现在,我可以让我的 bash 终端和 Ctrl-Shift-BF5 正常工作。

哦,正如其他海报所提到的,此信息的来源是 VSCode's documentation


M
Maksym Dudyk

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

Ctrl + j

l
leonbloy

更新:(2022 年 6 月,VsCode 1.67)

这里的许多答案(如 this one,高度赞成)依赖于设置 "terminal.integrated.shell.windows",现在是 deprecated

许多其他人(作为公认的)假设 Git for Windows 与 PATH 中的 bash.exe 一起安装 - 这不是当前推荐/默认的安装选项。

我目前的食谱:

将以下内容添加到您的设置中 (Ctrl-Shift-P -> Preferences: Open Settings (JSON) ),替换为您自己的 bash 路径:

  "terminal.integrated.profiles.windows": {
    "GitBash": {
      "path": "C:\\devel\\Git\\bin\\bash.exe",
      "icon": "terminal-bash"
    }
  },
  "terminal.integrated.defaultProfile.windows": "GitBash",

关闭当前终端并重新启动 VsCode。


这就是方法。几个月来我一直在寻找这个解决方案。谢谢!
C
Community

如果您已经拥有“bash”、“powershell”和“cmd” CLI,并且具有正确的路径设置,则可以通过以下方式从一个 CLI 切换到另一个 CLI。

Ctrl + ' :使用默认 CLI 打开终端窗口。 bash + enter :从您的默认/当前 CLI 切换到 bash CLI。 powershell + enter :从您的默认/当前 CLI 切换到 powershell CLI。 cmd + enter :从您的默认/当前 CLI 切换到 cmd CLI。

我使用的 VS Code 版本是 1.45.0


A
Addishiwot Shimels

在最新的稳定版本中,即 1.54,禁用 ConPTY 解决了我的问题。我正在写这篇文章,以防它也解决了你的问题。

{

 "terminal.integrated.shell.windows": "C:\\path\\to\\bin\\bash.exe",
 "terminal.integrated.windowsEnableConpty": false
}

B
Bill Tür stands with Ukraine

将此添加到 settings.json

{
    "terminal.integrated.profiles.windows": {
        "PowerShell": {
            "source": "Git Bash",
            "icon": "terminal-bash"
        },

m
mc51

VSCODE 1.63.2

即使结合这里的答案,我也无法让它发挥作用。也许,我有一个有点异国情调的设置。我会收到此错误,设置 "terminal.integrated.defaultProfile.windows": "Git Bash" 并且它仍然只会打开 Powershell 并且 Git Bash 无处可见:

https://i.stack.imgur.com/2kksD.png

我的解决方法是通过更改指向 bash 的路径来修改 Command Prompt 的条目,然后将其设置为默认值,如下所示:

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