starship:跨 shell 跨语言的智能 prompt

起因

shell prompt 需要显示:

  • 当前 git 分支 / 状态
  • 当前 Python venv / Node 版本
  • 上一命令 exit code
  • 是不是 ssh
  • 当前 k8s context

老办法:

  • bash 写 PS1='...' 一长串 escape
  • zsh 装 oh-my-zsh + theme
  • 每个 shell(bash / zsh / fish / nu)都要重新配

starship(Rust)是跨 shell 统一 prompt engine。一份 TOML 配置
所有 shell 受用 + 快 + 美观。

brew install starship
curl -sS https://starship.rs/install.sh | sh

# 集成
eval "$(starship init bash)"      # bash
eval "$(starship init zsh)"       # zsh
starship init fish | source       # fish

# 加到 rc file
echo 'eval "$(starship init zsh)"' >> ~/.zshrc

默认效果

~/projects/myapp on  feature/auth [✱]
❯

显示:

  • 路径
  • git 分支 + 修改标记
  • 自动检测项目类型(如 Node project 显示 node 版本,Rust 显示 cargo
    版本)

自定义 ~/.config/starship.toml

# 整体格式
format = """
$directory\
$git_branch$git_status\
$python$nodejs$rust\
$kubernetes\
$line_break\
$character"""

# 单组件配置
[directory]
truncation_length = 3
truncate_to_repo = true
style = "bold cyan"

[git_branch]
symbol = " "
style = "bold green"

[git_status]
modified = "*"
staged = "+"
untracked = "?"
ahead = "⇡${count}"
behind = "⇣${count}"
diverged = "⇕"

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

[nodejs]
symbol = " "
disabled = false

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

[character]
success_symbol = "[❯](green)"
error_symbol = "[❯](red)"

性能

starship 测量自己执行时间:

starship time

100-200ms 慢的话,prompt 觉得卡。常见原因:

  • git status 在大 repo 慢
  • python module check 慢

优化:

[git_status]
disabled = false
ignore_submodules = true       # 大 submodule repo 跳过

[python]
disabled = true                # 不显 python 版本

或者用 command_timeout = 500(毫秒)限制慢操作。

项目类型自动检测

starship 在每次按回车跑一次,判断当前目录类型:

  • package.json → 显 node 版本
  • pyproject.toml / requirements.txt → 显 python
  • Cargo.toml → rust
  • go.mod → go
  • Dockerfile → docker
  • Gemfile → ruby
  • ...

不在项目里时不显(不噪音)。

多线设计

format = """
[╭─](bold yellow) $username@$hostname $directory\
$git_branch$git_status$git_state$git_metrics
[╰─](bold yellow)$character"""
╭─ alice@laptop  ~/proj/myapp on  main
╰─❯

第二行只放 prompt 字符 → 命令永远在固定位置,长 prompt 不挤右侧。

right prompt

right_format = "$cmd_duration $time"

[cmd_duration]
min_time = 500     # > 500ms 才显
format = "took [$duration](bold yellow) "

[time]
disabled = false
format = '[$time]($style) '
time_format = "%R"
~/proj/myapp on  main                          took 2.3s  15:42
❯

长命令耗时 + 时间右侧显示。

通过 ssh 改色

[hostname]
ssh_only = true
format = "[$hostname](bold red) "

只在 ssh 里显主机名,本地不显。提醒"我在远程"。

kubernetes context

[kubernetes]
disabled = false
format = '⛵ [$context(\($namespace\))]($style) '
style = "purple"

[kubernetes.context_aliases]
"gke_my-project-prod_us-central1_cluster" = "prod"
"gke_my-project-staging_us-central1_cluster" = "stg"
⛵ prod(default) ~/k8s on  main ❯

不同 context 染色 → 误操作 prod 之前肉眼能注意到。

env_var

[env_var.AWS_PROFILE]
format = "AWS:[$env_value](bold blue) "
default = ""

显当前 AWS profile(避免 prod 误操作)。

与 zsh theme 对比

starship powerlevel10k oh-my-zsh themes
跨 shell ❌(zsh only)
速度 快(rust) 极快(缓存)
配置 TOML zsh wizard shell script
可读性
新人友好

p10k 在 zsh 里最快(用 instant prompt)。
starship 跨 shell + 简单配置。我用 starship。

fish 友好

fish 用户最爱 starship。fish_prompt 自定义很麻烦,starship 一行接管。

nushell

nushell 也支持:

# ~/.config/nushell/config.nu
$env.PROMPT_COMMAND = { || starship prompt }

我的精简配置

# ~/.config/starship.toml

format = """
$directory$git_branch$git_status$python$nodejs$kubernetes
$character"""

right_format = "$cmd_duration"

[directory]
truncation_length = 4
truncate_to_repo = true

[git_branch]
symbol = " "
format = "on [$symbol$branch]($style) "

[git_status]
format = '([\[$all_status$ahead_behind\]]($style)) '

[python]
symbol = "py "
format = '[$symbol(\($virtualenv\) )]($style)'

[nodejs]
symbol = "node "
detect_files = ["package.json"]

[kubernetes]
disabled = false
symbol = "⎈ "
format = '[$symbol$context]($style) '

[cmd_duration]
min_time = 1000
format = "[$duration]($style) "

[character]
success_symbol = "[❯](green)"
error_symbol = "[❯](red)"
vimcmd_symbol = "[❮](green)"

效果:

~/proj/myapp on  main [*+]  ⎈ prod
❯

干净 + 信息量足。

踩过的坑

  1. NerdFont 字符不显:图标方块乱码。换支持 NerdFont 的字体
    (JetBrainsMono Nerd / Hack Nerd / Iosevka)。

  2. 大 monorepo git status 慢:每次回车 100-500ms。
    disabled = true 或限 ignore_submodules = true

  3. conda env 没显:默认 starship 不显 conda。[conda] disabled = false

  4. 远程 ssh prompt 不换色:忘配 [hostname] ssh_only = true

  5. shell init 慢time zsh -i -c exit。starship init 一般 < 10ms,
    慢的话 starship 版本太老 / 配置 toml 错。

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

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

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

登录后参与评论。

还没有评论,来说两句。