Skip to content

PowerShell Terminal 美化指南

在 Windows 以 Windows Terminal + PowerShell 7 建立兼具可讀性與效率的開發終端:主題、字型、預測補全與圖示化檔案瀏覽一次完成。

概述

PowerShell 在 Windows 開發流程中通常同時承擔 CLI、Git 操作與工具鏈入口。若僅使用預設 prompt,容易缺乏上下文資訊(例如 Git 分支、錯誤狀態、語言版本),也無法提供直覺的指令建議。這會讓日常操作成本偏高。

以 Oh My Posh、Nerd Font、PSReadLine、Terminal-Icons 組合,可以將終端升級為「可視化提示 + 智慧補全 + 高辨識列表」的工作環境。重點不是純外觀,而是降低操作錯誤率與切換成本。

核心內容

四個核心元件的分工

元件角色主要價值
Oh My PoshPrompt 主題引擎顯示 Git 狀態、執行結果、執行環境資訊
Nerd Font圖示字型讓主題符號與檔案圖示正確渲染
PSReadLine命令列增強歷史預測、快捷鍵搜尋、補全
Terminal-Iconsls 圖示化以視覺方式快速辨識檔案類型

設定順序與依賴關係

正確順序是先安裝 Oh My Posh 與字型,再做 Profile 設定。若字型晚於主題設定,通常會看到方框字元。Profile 設定完成後,應以 . $PROFILE 立即驗證,並透過重開 Terminal 確認永久生效。

新版 Oh My Posh 建議直接使用 oh-my-posh init pwsh --config "<theme>" | Invoke-Expression。舊版 Get-PoshThemes 與部分環境變數用法在新安裝路徑下不保證可用。

PowerShell 與 VS Code 雙環境一致性

很多人只設定 Windows Terminal 字型,忽略 VS Code 整合終端,結果同一套 prompt 在不同終端顯示不一致。建議同步設定 VS Code 的 terminal.integrated.fontFamily,確保圖示、寬度與對齊一致。

關鍵要點

  • 優先使用 PowerShell 7(pwsh),避免與 Windows PowerShell 5.1 混用
  • 字型問題是最多見故障點,先確認 Nerd Font 再排查主題
  • 以主題名稱作為 --config 可快速切換;追求啟動效能可匯出本機主題檔
  • PSReadLine 建議啟用 HistoryAndPlugin,可同時取得歷史與外掛預測
  • 若同時使用 WSL,需分開管理 Linux shell 美化(見WSL2 Terminal 美化與功能強化)

實際應用

在多工具工作流中(Git、Node、Python、Kubernetes),一個資訊完整的 prompt 可以降低上下文切換成本。例如在切換 Node 版本管理工具(如Volta Node.js 版本管理)或跨 Windows/WSL 工作時,能快速辨識當前執行環境是否正確。

部署設定參考

以下為實作時可直接套用的關鍵設定片段。

安裝指令

powershell
winget install JanDeDobbeleer.OhMyPosh -s winget
oh-my-posh font install Hack
Install-Module PSReadLine -Force -SkipPublisherCheck
Install-Module -Name Terminal-Icons -Repository PSGallery -Force

PowerShell Profile 範例

powershell
# --- Oh My Posh ---
oh-my-posh init pwsh --config "craver" | Invoke-Expression

# --- Terminal-Icons ---
Import-Module -Name Terminal-Icons

# --- PSReadLine ---
Import-Module PSReadLine
Set-PSReadLineOption -EditMode Windows
Set-PSReadLineOption -PredictionSource HistoryAndPlugin
Set-PSReadLineOption -PredictionViewStyle InlineView
Set-PSReadLineKeyHandler -Key UpArrow -Function HistorySearchBackward
Set-PSReadLineKeyHandler -Key DownArrow -Function HistorySearchForward
Set-PSReadLineKeyHandler -Key Tab -Function Complete

VS Code 終端字型

json
{
  "terminal.integrated.fontFamily": "'Hack Nerd Font Mono'"
}

相關概念

來源