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

496

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


请阅读 https://dev59.com/M1cQ5IYBdhLWcg3wCvY4#50890703 以获取答案。 - AKS
31个回答

656
  1. https://git-scm.com/download/win下载并安装Git。

  2. 打开Visual Studio Code,按住Ctrl+`键打开终端。

    图片描述

  3. 使用Ctrl+Shift+P快捷键打开命令面板。

  4. 输入- 选择默认配置文件

  5. 从选项中选择Git Bash。

  6. 在终端窗口点击+图标。

  7. 新的终端现在将是一个Git Bash终端。请等待几秒钟加载Git Bash。

    图片描述

  8. 现在可以通过终端下拉列表在不同的终端之间切换。

    图片描述


8
这对我有用。我尝试了所有其他指南,手动添加默认的bash,但每次都会打开一个新的终端窗口。非常感谢。 - Glen
如果按下ctrl+`没有反应(即使已经安装了git并设置了用户设置json),那么请尝试这样做:ctrl+shift+P > 聚焦终端。 - olisteadman
5
请注意,现在显示为“选择默认配置文件”。 - Matt Vukas
2
这些解决方案都无法在新的VSCode版本1.60.0(用户设置)中工作:当VSCode启动时,我仍然有powershell。 - Ruslan Jackson
4
对我来说不起作用。 Git Bash没有显示在选项中。我在这里找到了答案。(其他答案在Git Bash名称中有一个空格字符,这不起作用 - 必须是没有空格的GitBash。) - mindplay.dk
显示剩余5条评论

421

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


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

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

Visual Studio Code command palate

然后这个标签页将显示默认设置在左侧,您的设置在右侧:

enter image description here

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

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

现在按下Ctrl + `以从Visual Studio Code打开终端,您将拥有Bash -

这里输入图片描述

此图展示了IT技术中的云计算。云计算是一种通过网络在多台计算机上共享资源的方式,可以提供各种服务,如存储、计算和软件等。使用云计算,用户可以根据自己的需要付费,并随时进行调整。云计算已经成为现代IT架构中不可或缺的一部分。

6
两个可能有帮助的信息:在编写路径时,请确保转义 JSON 中的反斜杠字符。同时请确保将64位版本的Git Bash包含到您的VSCode中,因为32位版本可能默认没有颜色。(只需从路径中删除“(x86)”部分即可)。 - Lajos Mészáros
2
除了Lajos的建议,如果你仍然无法看到终端打开,请尝试重新启动VSCode。 - adityah
5
我遇到了关于 "terminal.integrated.shell.windows": "C:\\Program Files\\Git\bin\bash.exe" 的问题,终端无法打开。尝试了 "terminal.integrated.shell.windows": "C:\\Program Files\\Git\\bin\\bash.exe",这样对我来说完美解决了问题。 - Dharmjeet Kumar
42
请确保链接到 bash.exe 而不是 git-bash.exe。后者会在 VS Code 之外作为单独的窗口打开终端,而 bash.exe 将在 VS Code 内部运行。 - Darius
2
如何在用户设置中找到settings.json,这里没有提到,我不得不在VSCode中进行一些尝试才找到它。我们需要在“搜索设置”文本框中输入“终端”,然后您将看到一个名为“在settings.json中编辑”的链接,您需要在此处单击。 - Ashu
显示剩余8条评论

112

更新于2023年3月9日:新的截图。选择终端 shell 或配置默认终端的下拉菜单稍有变化。按下 Ctrl-' 进行选择:

new pull down menu

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

Select Default Shell option

请记住它只列出了在您的%PATH%环境变量中的Shell。对于不在路径中的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 selection list

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

请参阅完整的Visual Studio Code shell参考。有很多实用的内容。


1
这就是我想要的答案。我想使用WSL Bash,但其他答案尝试使用随git一起提供的Bash。 - ahmadali shafiee
3
有没有一种方式可以打开特定的shell,而不将其设置为“默认”?我认为有一个“仅此一次打开'bash'”是有意义的。 - Brent Arias
@BrentArias 在 shell 旁边有一个向下箭头。 - neves
1
@RedGlyph现在下拉菜单在加号旁边。我会编辑上面的截图。 - neves
@neves 我已经尝试过了,但这只显示了“powershell”,这是终端打开时的默认设置。所以甚至没有命令提示符。我得在某个地方检查设置。我发现设置和使用 VSC 非常令人困惑。 - RedGlyph
显示剩余4条评论

49

按住 Ctrl + ` 键打开终端。 在终端里输入 bash 使用 Git Bash。 注意:确保你的机器已安装 Git Bash。

如果你想再次使用 PowerShell,在终端中输入 powershell。要使用 Windows 命令行,请在终端中输入cmd

所选设置将作为您的默认设置。


4
这会为所选答案增加更多的价值。 - pjdupreez
2
要“切换回PowerShell”,你可能需要从默认Shell内运行的Bash Shell中退出,然后输入“powershell”,因为Bash不知道什么是PowerShell。 - Lucas

19

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

"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并重新安装它(这会让我重新安装所有扩展并重做我的自定义设置!)


1
这真的帮了我很多。我按照之前的答案使bash工作了,但是我失去了所有的bash和git别名。这让它们回来了。谢谢。 - David Martin
1
这是我使用的配置: "terminal.integrated.shell.windows": "C:\Program Files\Git\bin\bash.exe", "terminal.integrated.shellArgs.windows": [ "--login", "-i" ] - Belal Mohammed
1
这个解决方案对我也起作用了。需要注意的是,在我的情况下,为了减少gitbash的延迟,我按照这篇文章https://dev59.com/Puk6XIcBkEYKwwoYEf8H中的步骤设置了一个新的HOME环境变量。为了在VSCode中正常工作,上面的答案完美地解决了问题。 - Rob B

18

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

    // ...
}


3
对于 "terminal.integrated.defaultProfile.windows": "gitbash" - 不接受此值。有效值为: "PowerShell","命令提示符","Git Bash","JavaScript Debug Terminal"。 - Raul
@Raul 这只是一个警告,你可以尝试重新加载 VS Code 窗口以检查效果。如果你为 "terminal.integrated.defaultProfile.windows" 设置的配置文件名称与 "terminal.integrated.profiles.windows" 中添加的名称相对应,那么它应该可以工作。 - rustyhu
我的设置中有以下这行代码,它在我的系统上可以运行:"terminal.integrated.defaultProfile.windows": "Git Bash"。 - SherylHohman
@SherylHohman 如果“Git Bash”配置文件已经存在并且可以正常工作,那就没问题了。但如果不存在,你也可以像我的回答介绍的那样添加自定义配置文件。 - rustyhu
1
重点是您提供的名称必须与现有(自定义)配置文件名称匹配。它可以被命名为任何您希望的名称,但名称在两个位置必须相同...这可能是@Raul错误的来源。请相应地编辑settings.json文件。 - SherylHohman
显示剩余5条评论

17

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

  1. 按下 Ctrl + Shift + P 打开Visual Studio Code命令面板。

  2. 在文本区域中输入 >preferences: Open Settings (JSON)

  3. 将以下行添加到在右侧窗格中显示的JSON文件的末尾。

    "terminal.integrated.shell.windows": "C:\\Program Files\\Git\\bin\\bash.exe"
    
  4. 关闭并重新打开您的Visual Studio Code实例。


10
我按照Paul DeCarlo的这篇教程中的步骤来使用Windows子系统Linux(WSL)中的Bash,代替Git Bash for Windows中自带的Bash。这些步骤与前面的答案相同,只需在用户设置中使用以下代码即可。

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

这对我来说第一次就成功了...这真是难得的事情。

1
确实罕见。尖端的。 - Christopher Painter

7

(2021年,VSC v.1.55.1)

如何将没有安装在默认路径下的Git Bash设置为默认终端:

  1. 使用 Ctrl+, 快捷键打开 Visual Studio Code 的 “Settings” 选项
  2. a) 在 “Search settings”(屏幕截图中红色方框) 中输入 “integrated automation” enter image description here b) 或者直接通过“Features”-“Terminal”(屏幕截图中蓝色方框)进入
  3. 单击任意一个“Edit in settings.json”
  4. enter image description here
  5. 在“terminal.integrated.shell.windows”字段中输入您的“bash.exe”位置 enter image description here

注意1: 这是一个JSON文件,所以请记得在您的路径中使用双重“\\”,而非单个“\”。

注意2: 不要混淆“bash.exe”(位于“bin”文件夹中)和“git-bash.exe”,在前面的情况下,bash终端将保留在VSC中,在后者中它将被外部打开。

enter image description here


7
这对我来说至少会使得Visual Studio Code打开一个新的Bash窗口作为外部终端。
如果您想要集成的环境,您需要指向Git安装目录中的bin文件夹内的sh.exe文件。
因此,配置应该写成C:\\<my-git-install>\\bin\\sh.exe。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接