如何在 macOS/Linux 安裝 Starship 終端提示符:設定 starship.toml 並接入 zsh 與 fish

安裝 Starship,設定 starship.toml,並在 zsh 或 fish 啟用跨 Shell 提示符。

終端裡要同時看清Git 分支、目錄深度和命令耗時,預設 PS1 往往資訊不足或配色雜亂。Starship 是 Rust 寫的跨 Shell 提示符,一條命令接入 zsh 或 fish,設定集中在 ~/.config/starship.toml。本教學走 macOS 與 Linux 的官方安裝路徑,並示範首次定制,面向 hereink 讀者常用的本地開發環境。

檢索詞是如何在 macOS/Linux 安裝 Starship 並設定 zsh 或 fish。讀完應能安裝二進位、寫入 init 行、編輯 starship.toml,並在新終端看到帶 Git 與耗時的提示符。

安裝brew · install.sh · cargo
設定~/.config/starship.toml
Shellzsh · fish · bash

Starship 解決什麼問題

比手寫 PS1 可維護

傳統 PS1 轉義序列難讀、難共享。Starship 用 TOML 描述模組(directory、git_branch、cmd_duration),改主題不必碰 Shell 腳本。

跨 Shell 同一套外觀

在 macOS 用 zsh、遠端機用 bash、本機 fish 時,同一份 starship.toml 可保持提示符一致,減少上下文切換成本。

官方文件與設定說明:starship.rs。安裝後執行 starship --version 確認版本。

在 macOS 與 Linux 安裝

  1. 1
    macOS(Homebrew,推薦)

    執行 brew install starship。驗證:which starship 與 starship --version。

  2. 2
    Linux 發行版套件

    Debian/Ubuntu 可先添加官方倉庫再 apt install starship;Fedora:dnf install starship;Arch:pacman -S starship 或 AUR。

  3. 3
    通用安裝腳本

    curl -sS https://starship.rs/install.sh | sh。通常需把 ~/.local/bin 加入 PATH。

  4. 4
    可選:cargo 安裝

    cargo install starship --locked。編譯較慢,適合無預編譯包的環境。

接入 zsh 或 fish

# zsh — 追加到 ~/.zshrc
eval "$(starship init zsh)"

# fish — 追加到 ~/.config/fish/config.fish
starship init fish | source

# bash — 追加到 ~/.bashrc
eval "$(starship init bash)"

儲存後新開終端或 source 對應設定檔。若提示符未變,檢查 starship 是否在 PATH 中。

編輯 ~/.config/starship.toml

模組作用示例片段
[directory]目前路徑,可截斷長路徑truncation_length = 3
[git_branch]顯示 Git 分支與狀態symbol = " "
[cmd_duration]慢命令耗時min_time = 500(毫秒)
format全局模組順序format = "$directory$git_branch$character"
適合
日常在 zsh/fish 寫程式、頻繁切換 Git 倉庫、希望提示符輕量且可版本控制的開發者。
不適合
必須嚴格相容舊式 PS1 的企業跳板機;或終端無法裝 Nerd Font 且不接受純文字符號。
安裝後提示 command not found: starship?

二進位不在 PATH。Homebrew 用戶確認 shellenv;install.sh 用戶把 ~/.local/bin 寫入 .zshrc 的 export PATH。

和 Oh My Zsh 主題衝突嗎?

啟用 Starship 後應關閉或註解 OMZ 的 ZSH_THEME,只保留 starship init 一行。

設定改了不生效?

確認編輯的是 ~/.config/starship.toml。TOML 語法錯誤時 Starship 會回退預設;可用 starship config 開啟編輯器。

終端配好後,對齊一張架構圖

本地環境順手之後,服務邊界仍適合攤在同一張白板上。見 如何建立空間。

立即開會