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

    // ...
}

其他回答

将此添加到settings.json中

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

更新:(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。

更新:较新版本的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参考。有很多肉的东西。

如果你已经有“bash”,“powershell”和“cmd”命令行,并且有正确的路径设置,那么可以通过以下方式从一个命令行切换到另一个命令行。

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

我使用的VS Code版本是1.45.0

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

Ctrl + j