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


当前回答

(2021, VSC v.1.55.1)

如何添加Git Bash为默认终端,对于那些安装他们的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中,第二种情况下它将在外部打开。

其他回答

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

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

因此,配置应该是C:\\<my-git-install>\\bin\\sh.exe。

这取决于你是只在当前用户还是所有用户中安装了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可以检测并在配置终端菜单中列出已安装的Git Bash:选择默认配置文件,正如许多其他答案已经描述的那样,但这从未发生在我身上。对于那些不像我这么幸运的人,你可以在Visual Studio Code的设置中添加自定义配置文件。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",

    // ...
}

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

这里的许多答案(就像这个,高度好评)依赖于设置“terminal.integrated.shell.windows”,现在已经弃用了。

还有许多其他的(被接受的)假设Git for Windows在PATH中安装bash.exe -这不是目前推荐/默认的安装选项。

我现在的食谱是:

添加以下到您的设置(Ctrl-Shift-P ->首选项:打开设置(JSON)),替换为您自己的bash路径:

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

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

Vs代码1.60.0

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

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

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

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