参加你附近的 ,了解 VS Code 中的 AI 辅助开发。

终端配置文件

终端配置文件是平台特定的 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 右侧的配置按钮。这将在你的设置中添加一个新条目,你可以在 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 的维基

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 脚本中,因为 rc 脚本在非登录 Shell 中运行。

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

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

© . This site is unofficial and not affiliated with Microsoft.