starship:用一份配置美化任何 shell 的提示符(bash/zsh/fish/PowerShell)

起因

我在 macOS 用 zsh + oh-my-zsh、Linux 服务器用 bash、Windows 偶尔用
PowerShell。每个 shell 一套提示符配置:颜色、git branch 显示、Python
venv 提示、kubectl context… 三套配置一改全乱。

starship 是 Rust 写的跨 shell 提示符生成器:一份 TOML 配置文件,
所有 shell 都能用同一份。

安装

# 一行装
curl -sS https://starship.rs/install.sh | sh

# 或包管理器
brew install starship
sudo apt install starship   # Debian 12+ / Ubuntu 24.04+
scoop install starship      # Windows

starship --version

接到 shell

# bash: ~/.bashrc 末尾
eval "$(starship init bash)"

# zsh: ~/.zshrc 末尾
eval "$(starship init zsh)"

# fish: ~/.config/fish/config.fish 末尾
starship init fish | source

# PowerShell: $PROFILE
Invoke-Expression (&starship init powershell)

重启 shell,提示符立刻变好看。

默认行为

启动后默认显示:

  • 当前目录(智能截断 home / 项目根)
  • Git 分支 + 状态(修改 / 新文件 / 待 push)
  • 当前语言版本(在该项目目录自动显示 Python/Node/Go/Rust 版本)
  • exit code(非 0 时红色显示)
  • 命令耗时(> 2s 自动显示)

不用任何配置就比 default 强 10 倍。

个性化 ~/.config/starship.toml

我用的(基于官方 nerd-font symbol preset 简化):

format = """
$directory\
$git_branch\
$git_status\
$python\
$nodejs\
$golang\
$rust\
$kubernetes\
$cmd_duration\
$line_break\
$character\
"""

[character]
success_symbol = "[➜](bold green)"
error_symbol = "[✗](bold red)"
vimcmd_symbol = "[V](bold yellow)"

[directory]
truncation_length = 3        # 只显示最后 3 段
truncate_to_repo = true       # 进 repo 后只显示从 repo 根的相对路径
style = "bold cyan"

[git_branch]
symbol = " "
style = "purple"

[git_status]
ahead = "⇡${count}"
behind = "⇣${count}"
diverged = "⇕⇡${ahead_count}⇣${behind_count}"
untracked = "?${count}"
modified = "✱${count}"
staged = "+${count}"
deleted = "✘${count}"
style = "yellow"

[cmd_duration]
min_time = 2_000
format = "took [$duration]($style) "
style = "yellow"

[python]
symbol = " "
format = '[${symbol}${pyenv_prefix}(${version} )(\($virtualenv\) )]($style)'
style = "bold yellow"

[nodejs]
symbol = " "
format = "[${symbol}${version}]($style) "
style = "bold green"

[kubernetes]
disabled = false
format = '[$symbol$context( \($namespace\))]($style) '
style = "bold blue"

# 关一些少用的模块加速
[aws]
disabled = true
[gcloud]
disabled = true
[package]
disabled = true

看效果

~/projects/myapp on  feature/auth ✱2 ?1
  3.12.0 (.venv)  prod  default took 4.5s
➜

让符号显示完整

starship 用了大量 nerd-font 符号(、、、)。终端必须装 Nerd Font
字体才能正常显示:

# macOS
brew tap homebrew/cask-fonts
brew install --cask font-jetbrains-mono-nerd-font

# Linux
mkdir -p ~/.local/share/fonts
cd ~/.local/share/fonts
curl -fLO https://github.com/ryanoasis/nerd-fonts/releases/latest/download/JetBrainsMono.zip
unzip JetBrainsMono.zip
fc-cache -fv

# 然后终端 settings 字体改成 'JetBrainsMono Nerd Font'

不装 Nerd Font 也能用——把 symbol 改成普通字符或留空。

跨机器同步配置

~/.config/starship.toml 放进 dotfiles repo:

cd ~/dotfiles
ln -sf $(pwd)/starship.toml ~/.config/starship.toml
git add starship.toml
git commit -m 'starship config'

新机器:

git clone https://github.com/me/dotfiles
ln -sf $(pwd)/dotfiles/starship.toml ~/.config/starship.toml

一次配置,所有机器一致。

性能

starship 单次 prompt 渲染 < 30ms(Rust + 并行模块)。
慢命令后 prompt 也立即响应。

# 看哪个模块拖时间
STARSHIP_LOG=debug starship prompt 2>&1 | grep -i 'time'

发现慢模块 → 关掉或加 disabled = true

与 oh-my-zsh / Spaceship 等对比

工具 速度 跨 shell 配置语法
starship 极快(Rust) ✅ 全支持 TOML
oh-my-zsh themes 慢(zsh 脚本) 仅 zsh shell
Spaceship 中(zsh script) 仅 zsh zsh vars
pure 极快 仅 zsh minimal
p10k 极快(C) 仅 zsh wizard

如果你只用 zsh,p10k 配置体验更好;多 shell 选 starship。

效果

  • 三台机器 + 两个 shell 同步配置,零差异
  • 提示符里直接看到 git branch / venv / Node 版本,少跑 N 个 git status
  • 命令耗时显示让我知道哪些命令该 background 跑
  • 同事看见后被安利的成功率约 50%

踩过的坑

  1. 服务器 SSH 上没 Nerd Font → 各种"豆腐块"。starship preset plain-text-symbols 切到纯文本符号 preset。

  2. WSL 启动慢:默认每次 prompt 都查 git。scan_timeout = 10
    限制超时;大仓库 disable git 模块只保留 branch。

  3. tmux 里颜色错:tmux 设 set -g default-terminal "tmux-256color"

  4. set -ga terminal-overrides ',xterm-256color:Tc' 启用真彩色。

  5. PowerShell 上 modulator 不显示:Windows Terminal 字体必须设为
    CaskaydiaCove NF 之类的 Nerd Font 变体。系统全局字体不算。

  6. zoxide / direnv 等其它工具的钩子顺序:starship init 应在最后,
    否则它的 hook 被覆盖。

精确评价 共 0 人评价
可复现性
可复现 · 0 不可复现 · 0
文风
文风流畅 · 0 文风晦涩 · 0
立场
支持 · 0 反对 · 0

登录后即可对本帖作出评价。

评论区 0 条 · 所有人可在此交流

登录后参与评论。

还没有评论,来说两句。