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


当前回答

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

要做到这一点,我只需要在Visual Studio Code的设置中指定这个特定的Bash可执行文件(而不是Git-Bash)的路径:

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


PS:为了确保你的Ubuntu on Bash可执行文件安装在你的Windows机器上,打开命令提示符(搜索:cmd)并运行:

在bash.exe

其他回答

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

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

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

将此添加到settings.json中

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

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


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

然后打开Visual Studio Code,使用Ctrl + Shift + p打开命令面板,然后输入“open user setting”,然后从下拉菜单中选择“open user Settings”。

然后这个选项卡会打开,左边是默认设置,右边是你的设置:

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

注意:“C:\\Program Files\Git\bin\bash.exe”是Git安装时bash.exe文件所在的路径。如果您使用的是Windows子系统for Linux (WSL) Bash shell,路径将是“C:\Windows\System32\ Bash .exe”

现在按Ctrl + '从Visual Studio Code打开终端。你会有巴斯

我的VS Code版本:1.56.1 (Code——version)

一体化终端配置的用户设置:

Ctrl + Shift + P 用户类型: 首选项:打开用户设置 点击:打开设置(JSON)按钮(靠近右上角)

settings.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

将显示废弃的警告。但是,要使所选shell(这里是github)在启动时成为默认shell,则需要此配置。

过滤问题

在PROBLEMS选项卡上,在输入字段旁边,单击过滤器图标,我选中了“Show Active File Only”选项,以便在处理其他任何事情时消除这个已弃用的错误。

我尝试了上面的答案,因为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的文档。