尝试在 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 右侧的配置按钮以作为基础。这将为您的设置添加一个新条目,您可以在 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 脚本中,因为 rc 脚本在非登录 shell 上运行。

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

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