🚀 在 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 右侧的配置按钮以基于该 shell 创建。这将在您的设置中添加一个新条目,可以在您的 settings.json 文件中手动调整。

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

{
  "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。

提示:路径、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 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 等)。

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