終端機設定檔
終端機設定檔 (Terminal profiles) 是針對特定平台的 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 環境變數所示。您可以前往變數參考 (Variables Reference) 主題查看可用變數列表。
執行 Terminal: Select Default Profile 命令來設定您的預設設定檔,此命令也可透過「新增終端機」下拉式選單存取。

預設終端機設定檔 Shell 在 Linux 和 macOS 上預設為 $SHELL,在 Windows 上則為 PowerShell。VS Code 會自動偵測大多數標準 Shell,並可將其設定為預設值。
設定終端機設定檔
若要建立新的設定檔,請執行 Terminal: Select Default Profile 命令,並點擊 Shell 右側的設定按鈕以作為基礎。這將在您的設定中新增一個項目,您可以在 settings.json 檔案中手動調整它。
設定檔可以使用 path 或 source 建立,並支援一組選擇性的引數。source 僅適用於 Windows,可用於讓 VS Code 偵測 PowerShell 或 Git 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 皆支援解析變數 (resolving variables)
預設設定檔可使用 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,但除非透過 Terminal: Select Default 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:\cygwin 或 C:\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 新增為設定檔,請在「新增終端機」下拉式選單中選擇 Select Default Profile,並選取 Windows PowerShell 項目。這將完成設定檔的配置並將其設為預設值。
WSL
在本地機器執行 VS Code 時,Windows Subsystem for Linux (WSL) 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 (login 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。這會助長「不良習慣」,例如在 profile 指令碼中初始化別名 (alias),而這些別名本應放在執行於非登入 Shell 的 rc 指令碼中。
針對此問題有兩個直接修正方式。第一是將 "terminal.integrated.inheritEnv": false 設為 false,這將從終端機環境中剔除大部分環境變數(除了一些重要的變數,如 HOME、SHELL、TMPDIR 等)。
另一個修正方式是透過建立終端機設定檔並將其 args 設為 [],使終端機不再以登入 Shell 執行。若採用此修正方式,請確保將 profile 指令碼中的任何別名移至您的 ~/.bashrc/~/.zshrc 檔案中,因為別名僅適用於設定它們的 Shell。