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


当前回答

我的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”选项,以便在处理其他任何事情时消除这个已弃用的错误。

其他回答

这个答案与投票最多的答案相似,但有一个重要的区别:之前关于这个问题的很多答案都集中在运行Git Bash上,而我的答案则集中在运行WSL Bash上。

在Windows 10机器上启用Windows子系统for Linux。 打开Visual Studio Code,按住Ctrl + '打开终端。 使用Ctrl + Shift + P打开命令面板。 类型—选择“Default Shell”。 从选项中选择WSL Bash(不是Git Bash)。

单击终端窗口中的+图标。新终端现在将是一个WSL Bash终端!

VSCODE 1.63.2

即使把这里的答案结合起来,我也不能让它工作。也许,我有一个奇异的设置。我会得到这个错误,设置"terminal.integrated.defaultProfile.windows": "Git Bash",它仍然只会打开Powershell, Git Bash无处可见:

我的解决方法是修改命令提示符的入口,通过改变它指向bash的路径,然后将其设置为默认值,如下所示:

"terminal.integrated.profiles.windows": {
    "PowerShell": {
        "source": "PowerShell",
        "icon": "terminal-powershell"
    },
    "Command Prompt": {
        "path": [
            "C:\\Path\\To\\Git\\bin\\bash.exe",
            // "${env:windir}\\Sysnative\\cmd.exe",
            // "${env:windir}\\System32\\cmd.exe"
        ],
        "args": [],
        "icon": "terminal-cmd"
    
},
"terminal.integrated.defaultProfile.windows": "Command Prompt",

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

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

我使用的VS Code版本是1.45.0

最新的VS代码:

如果你看不到设置。进入菜单文件->首选项->设置(或按Ctrl+,) 设置出现,见两个选项卡用户(默认选择)和工作空间。进入“用户->功能->终端” 终端部分出现,请参见settings.json中的链接编辑。点击并添加"terminal.integrated.shell.windows": "C:\\Program Files\\Git\\bin\\bash.exe" 保存并重新启动VS代码。

Bash终端将反映在终端上。

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

Ctrl + j