为 Windows 版 VSCode 添加 Git Bash

vscode / git / bash / windows

相信有不少朋友在 Windows 下使用 VSCode 和 git 进行开发,我也一直使用 git bash 作为 VSCode 的默认终端,虽然这个 bash 不太完整,但也基本够用,不过我在两台 pc 上 配置 git bash 为 VSCode 的默认终端时都遇到了问题。

问题

为 VSCode 添加 Git Bash,目前的版本,需要在 setting 中设置 profile:

{
  "terminal.integrated.profiles.windows": {
    "PowerShell": {
      "source": "PowerShell",
      "icon": "terminal-powershell"
    },
    "Command Prompt": {
      "path": [
        "${env:windir}\\Sysnative\\cmd.exe",
        "${env:windir}\\System32\\cmd.exe"
      ],
      "args": [],
      "icon": "terminal-cmd"
    },
    "Git Bash": {
      "path": "C:\\path\\to\\git\\bin\\bash.exe",
      "icon": "terminal-bash"
    }
  },
  "terminal.integrated.tabs.enabled": true,
  "terminal.integrated.defaultProfile.windows": "Git Bash"
}

不过这样配置以后,「聪明」的 VSCode 会告诉你,他找不到 Git Bash,而且确实我们也无法从新建终端的下拉菜单中选择 Git Bash

git bash is not accepted

重启也是一样的

解决

解决这个问题,「Git Bash」的 profile 名称中不能有空格,也就是说,我们需要改名为「Git-Bash」:

{
  "terminal.integrated.profiles.windows": {
    "PowerShell": {
      "source": "PowerShell",
      "icon": "terminal-powershell"
    },
    "Command Prompt": {
      "path": [
        "${env:windir}\\Sysnative\\cmd.exe",
        "${env:windir}\\System32\\cmd.exe"
      ],
      "args": [],
      "icon": "terminal-cmd"
    },
    "Git-Bash": {
      "path": "C:\\path\\to\\git\\bin\\bash.exe",
      "icon": "terminal-bash"
    }
  },
  "terminal.integrated.tabs.enabled": true,
  "terminal.integrated.defaultProfile.windows": "Git-Bash"
}

重启以后就可以了。

结论

别问我为什么「Command Prompt」可以有空格……