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


当前回答

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年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参考。有很多肉的东西。

我尝试了上面的答案,因为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-B或F5工作没有问题。

哦,正如其他海报提到的,这些信息的来源是VSCode的文档。

将此添加到settings.json中

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

这取决于你是只在当前用户还是所有用户中安装了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在我看来完全一样。它们之间应该没有区别。