Powerline 是跨 Shell (bash/zsh/fish)、終端機、Vim/Neovim、tmux、IPython 等環境的 統一狀態列/提示符引擎,以 Powerline Symbols 字體呈現漂亮分段箭頭、Git 分支、虛擬環境、錯誤碼、時間等資訊,大幅提升命令列辨識度與開發效率。本文記錄 macOS 上從零建置 Powerline 完整流程。
# 1. 安裝 powerline-status (核心套件)
pip3 install --user powerline-status
# 2. 查詢安裝路徑 (後續配置需用)
python3 -c "import powerline; print(powerline.__file__)"
# 輸出範例: /Users/username/Library/Python/3.9/lib/python/site-packages/powerline/__init__.py
# 記下 site-packages 目錄: /Users/username/Library/Python/3.9/lib/python/site-packages
編輯 `~/.bash_profile` (bash) 或 `~/.zshrc` (zsh, macOS Catalina+ 預設):
# Powerline 啟動腳本路徑 (依實際安裝位置調整)
POWERLINE_PATH="/Users/username/Library/Python/3.9/lib/python/site-packages"
if [ -f "$POWERLINE_PATH/powerline/bindings/bash/powerline.sh" ]; then
source "$POWERLINE_PATH/powerline/bindings/bash/powerline.sh"
fi
# zsh 用戶改 source "$POWERLINE_PATH/powerline/bindings/zsh/powerline.zsh"
重新載入:`source ~/.zshrc` 或重開終端機。
Powerline 使用 Unicode 私用區字元繪製分段箭頭,**必須安裝打過補丁的等寬字體**,否則顯示亂碼/方塊字。
# 方案 A: 官方字體倉庫 (含 30+ 熱門字體)
git clone https://github.com/powerline/fonts.git --depth=1
cd fonts && ./install.sh
# 會安裝到 ~/Library/Fonts (macOS)
# 方案 B: Nerd Fonts (含 Powerline + 開發圖示, 推薦)
brew tap homebrew/cask-fonts
brew install --cask font-meslo-lg-nerd-font # 或 font-fira-code-nerd-font 等
終端機字體設定:
確認 Vim 有 Python 支援:`vim --version | grep +python3` (Neovim 內建)。
編輯 `~/.vimrc` (或 `~/.config/nvim/init.vim`):
" 1. 加入 Powerline runtime path (依實際路徑調整)
set rtp+=/Users/username/Library/Python/3.9/lib/python/site-packages/powerline/bindings/vim
" 2. 基礎環境設定
set nocompatible
set t_Co=256
set laststatus=2 " 永遠顯示狀態列
set noshowmode " 隱藏預設 --INSERT-- 等模式文字
set guifont=MesloLGS\ NF:h14 " GUI 模式字體
" 3. 載入 Powerline
python3 from powerline.vim import setup as powerline_setup
python3 powerline_setup()
python3 del powerline_setup
" 4. 插入模式無延遲 (提升體驗)
if !has('gui_running')
set ttimeoutlen=10
augroup FastEscape
autocmd!
au InsertEnter * set timeoutlen=0
au InsertLeave * set timeoutlen=1000
augroup END
endif
Powerline 使用 JSON 配置段落、顏色、模組。複製預設配置到用戶目錄修改:
mkdir -p ~/.config/powerline
cp -r /Users/username/Library/Python/3.9/lib/python/site-packages/powerline/config_files/* ~/.config/powerline/
# 編輯 ~/.config/powerline/themes/shell/default.json 等
常用主題:`default`、`default_leftonly`、`solarized`。可自訂顯示 Git 分支、虛擬環境、Kubernetes context、AWS profile、時間、電池等級等。
| 現象 | 可能原因 | 解決 |
|---|---|---|
| 提示符顯示方塊/亂碼 | 字體未安裝或終端機未設定 Powerline 字體 | 重新安裝字體、確認 Non-ASCII Font 設定、重啟終端機 |
| Vim 報錯 ImportError | Vim 無 Python3 支援 / 路徑錯誤 | `brew install vim` 或 `brew install neovim`;檢查 rtp 路徑 |
| 顏色異常 | 終端機非 256 色/True Color | iTerm2: Profiles → Terminal → Report Terminal Type: xterm-256color; 啟用 True Color |
| 啟動慢 | Powerline Python 進程啟動延遲 | 改用 Rust 重寫版 powerline-rs / starship (更快、無 Python 依賴) |