在 VS Code 中试用

终端配置文件

终端配置文件是特定于平台的 Shell 配置,包含可执行文件路径、参数和其他自定义设置。默认情况下,会自动检测到多个配置文件,您可以自定义或添加新的配置文件。

配置文件示例

{
  "terminal.integrated.profiles.windows": {
    "Custom Init": {
      "path": "pwsh.exe",
      "args": [
         "-noexit",
         "-file",
         "${env:APPDATA}\\PowerShell\\custom-init.ps1"
      ]
    }
  },
  "terminal.integrated.defaultProfile.windows": "Custom Init"
}

如上例所示,您可以在终端配置文件中使用变量,例如 APPDATA 环境变量。在变量参考主题中,有可用变量的列表。

通过运行终端: 选择默认配置文件命令配置您的默认配置文件,此命令也可以通过新终端下拉菜单访问。

Select Default Profile is located at the bottom of the dropdown menu attached to the new terminal button

默认终端配置文件的 Shell 在 Linux 和 macOS 上默认为 $SHELL,在 Windows 上默认为 PowerShell。VS Code 将自动检测大多数标准 Shell,然后可以将它们配置为默认 Shell。

配置配置文件

要创建新的配置文件,请运行终端: 选择默认配置文件命令,然后激活右侧 Shell 旁边的配置按钮。这将在您的设置中添加一个新条目,您可以在 settings.json 文件中手动调整。

可以使用 pathsource 以及一组可选参数创建配置文件。source 仅在 Windows 上可用,可用于让 VS Code 检测 PowerShellGit Bash 的安装。或者,可以使用直接指向 Shell 可执行文件的 path。以下是一些配置文件配置示例

{
  "terminal.integrated.profiles.windows": {
    "PowerShell -NoProfile": {
      "source": "PowerShell",
      "args": ["-NoProfile"]
    }
  },
  "terminal.integrated.profiles.linux": {
    "zsh (login)": {
      "path": "zsh",
      "args": ["-l"]
    }
  }
}

配置文件中支持的其他参数包括

  • overrideName:一个布尔值,指示是否将检测正在运行的程序的动态终端标题替换为静态配置文件名。
  • env:一个映射,定义环境变量及其值,将变量设置为 null 以从环境中删除它。可以使用 terminal.integrated.env.<platform> 设置为所有配置文件配置此项。
  • icon:用于配置文件的图标 ID。
  • color:用于为图标设置样式的配色主题 ID。

提示: Path、args 和 env 都支持解析变量

可以使用 terminal.integrated.defaultProfile.* 设置手动定义默认配置文件。此设置应设置为现有配置文件的名称

{
  "terminal.integrated.profiles.windows": {
    "my-pwsh": {
      "source": "PowerShell",
      "args": ["-NoProfile"]
    }
  },
  "terminal.integrated.defaultProfile.windows": "my-pwsh"
}

提示:集成终端 Shell 以 VS Code 的权限运行。如果需要使用提升的(管理员)或其他权限运行 Shell 命令,请在终端中使用 runas.exe 等平台实用程序。

删除内置配置文件

要删除内置配置文件并阻止其显示在新终端下拉菜单中,请将配置文件的名称设置为 null。例如,要在 Windows 上删除 Git Bash 配置文件,请使用此设置

{
  "terminal.integrated.profiles.windows": {
    "Git Bash": null
  }
}

配置任务/调试配置文件

默认情况下,任务/调试功能将使用默认配置文件。如果您的默认配置文件有繁重的 PowerShell 启动脚本或不符合 POSIX 标准的 Shell 等,这可能不是理想选择。要配置仅在调试/任务功能中使用的配置文件,请使用 terminal.integrated.automationProfile.<platform> 设置

{
  "terminal.integrated.defaultProfile.osx": "fish",
  // Use a fully POSIX-compatible shell and avoid running a complex ~/.config/fish/config.fish
  // for tasks and debug
  "terminal.integrated.automationProfile.osx": {
    "path": "/bin/sh"
  }
}

特定配置文件的键盘快捷方式

通过专用键盘快捷方式使用特定配置文件启动终端可以使用 workbench.action.terminal.newWithProfile 命令实现。此命令接受配置文件名和可选位置作为参数。例如,将 Ctrl+Shift+T 绑定到使用 zsh 配置文件打开终端

{
  "key": "ctrl+shift+t",
  "command": "workbench.action.terminal.newWithProfile",
  "args": {
    "profileName": "zsh",
    "location": "editor"
  }
}

不安全配置文件检测

某些 Shell 默认安装在不安全的路径中,例如在 Windows 环境下可能被其他用户写入的路径。VS Code 仍然会检测到这些 Shell,但在通过终端: 选择默认配置文件命令明确配置之前,不会将其显示为正常的配置文件。配置不安全的配置文件时,在添加之前会有警告

Shells with unsafe paths like c:\msys64 will show a warning before you can use the detected profile

Cmder

Cmder 本身是一个终端,但您可以在 VS Code 中使用以下配置文件来使用 Cmder Shell

{
  "terminal.integrated.profiles.windows": {
    "cmder": {
      "path": "C:\\WINDOWS\\System32\\cmd.exe",
      "args": ["/K", "C:\\cmder\\vendor\\bin\\vscode_init.cmd"]
    }
  },
  "terminal.integrated.defaultProfile.windows": "cmder"
}

设置 CMDER_ROOT 环境变量后,应自动检测到此配置文件。如果安装在 C:\cmder,它也会被检测为不安全配置文件。有关详细信息,您可以参考Cmder 的 wiki

Cygwin

Cygwin 本身是一个终端,但您可以在 VS Code 中使用以下配置文件来使用 Cygwin Shell

{
  "terminal.integrated.profiles.windows": {
    "Cygwin": {
      "path": "C:\\cygwin64\\bin\\bash.exe",
      "args": ["--login"]
    }
  },
  "terminal.integrated.defaultProfile.windows": "Cygwin"
}

当安装在默认路径 C:\cygwinC:\cygwin64 时,应自动检测到此配置文件为不安全配置文件

Git Bash

VS Code 使用 bash.exe(Shell)而不是 git-bash.exe(终端)时,Git Bash 的一个限制是历史记录不会跨 Shell 会话保留。您可以通过将以下内容添加到 ~/.bashrc~/.bash_profile 文件来解决此问题

export PROMPT_COMMAND='history -a'

这将导致 Shell 在每次打印提示符时调用 history -a,从而将当前会话命令刷新到历史记录文件中。

MSYS2

可以使用以下配置文件配置 MSYS2 的 bash Shell

{
  "terminal.integrated.profiles.windows": {
    "bash (MSYS2)": {
      "path": "C:\\msys64\\usr\\bin\\bash.exe",
      "args": ["--login", "-i"],
      "env": { "CHERE_INVOKING": "1" }
    }
  }
}

CHERE_INVOKING 环境变量用于告诉登录初始化脚本保留工作目录,而不是在 $HOME 处打开。

当安装在默认路径 C:\\msys64 时,应自动检测到此配置文件为不安全配置文件

Windows PowerShell

安装 PowerShell 6+ 时,默认情况下 Windows PowerShell 不包含在配置文件列表中。要将 Windows PowerShell 添加为配置文件,请在新终端下拉菜单中选择选择默认配置文件选项,然后选择 Windows PowerShell 项。这将配置该配置文件并将其设置为您的默认配置文件。

WSL

在本地计算机上运行 VS Code 时,应自动检测到 Windows Subsystem for Linux Shell。根据您的设置,如果安装了大量发行版,这可能会很麻烦。要更精细地控制 WSL 配置文件,可以使用 terminal.integrated.useWslProfiles 设置禁用自动检测,然后这里是如何手动配置 WSL Shell 的示例

{
  "terminal.integrated.profiles.windows": {
    "Debian (WSL)": {
      "path": "C:\\WINDOWS\\System32\\wsl.exe",
      "args": [
        "-d",
        "Debian"
      ]
    }
  }
}

常见问题

为什么终端的 $PATH 环境变量中存在重复路径,和/或为什么它们在 macOS 上是反向的?

这可能发生在 macOS 上,因为终端使用 VS Code 的环境启动。VS Code 首次启动时,为了获取您的“开发环境”,它将配置的 Shell 作为登录 Shell 启动,这将运行您的 ~/.profile/~/.bash_profile/~/.zprofile 脚本。现在终端启动时,它也作为登录 Shell 运行,这将把标准路径放在前面(例如,/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin)并重新初始化您的 Shell 环境。

为了更好地理解,您可以在操作系统内置终端中启动一个内部登录 Shell 来模拟正在发生的情况

# Add /test to the beginning of $PATH
export PATH=/test:$PATH
# Echo $PATH, /test should be at the beginning
echo $PATH
# Run bash as a login shell
bash -l
# Echo $PATH, the values should be jumbled
echo $PATH

不幸的是,与 Linux 不同,独立的 macOS 终端默认都以登录 Shell 运行,因为用户登录系统时 macOS 不运行登录 Shell。这鼓励了“不良行为”,例如在配置文件脚本中初始化别名,而这些别名应该位于 rc 脚本中,因为它在非登录 Shell 上运行。

有两种直接的解决方法。第一种是将 "terminal.integrated.inheritEnv": false,这将从终端环境中剥离大多数环境变量,除了一些重要的变量(如 HOMESHELLTMPDIR 等)。

另一种解决方法是创建终端配置文件并将其 args 设置为 [],以阻止终端运行登录 Shell。如果您选择此解决方法,您需要确保将配置文件脚本中的任何别名移动到 ~/.bashrc/~/.zshrc 文件中,因为别名仅适用于设置它们的 Shell。