Starship 跨 Shell 提示符¶
为什么要学 Starship¶
Starship 是一个快速、可定制的跨 Shell 提示符工具,用 Rust 编写。它能自动检测当前目录的开发环境(Git 状态、编程语言版本、云平台、容器等)并在提示符中优雅显示。无论你使用 Bash、Zsh、Fish、PowerShell 还是 Nushell,Starship 都能提供一致的美观体验,告别手动配置各种 PS1 的痛苦。
核心概念¶
| 概念 | 白话解释 | 用途 |
|---|---|---|
| Module | 模块 | 提示符中的一个信息段(如 Git、Python 版本) |
| Preset | 预设 | 预定义的主题配置 |
| Format | 格式 | 模块的排列方式 |
| Style | 样式 | 颜色和文字样式 |
| Condition | 条件 | 模块何时显示 |
安装配置¶
安装¶
# 通用安装脚本
curl -sS https://starship.rs/install.sh | sh
# macOS
brew install starship
# Cargo
cargo install starship
# Windows
winget install --id Starship.Starship
激活¶
# 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)
# Nushell
mkdir ~/.cache/starship
starship init nu | save -f ~/.cache/starship/init.nu
# 在 config.nu 中 source ~/.cache/starship/init.nu
配置文件¶
# ~/.config/starship.toml
# 基础设置
add_newline = true # 提示符前空一行
command_timeout = 500 # 命令超时(毫秒)
# 提示符格式
format = """
$username\
$hostname\
$directory\
$git_branch\
$git_status\
$python\
$nodejs\
$rust\
$golang\
$docker_context\
$cmd_duration\
$line_break\
$character"""
快速上手¶
使用预设¶
# 查看所有预设
starship preset --list
# 应用预设
starship preset nerd-font-symbols -o ~/.config/starship.toml
starship preset tokyo-night -o ~/.config/starship.toml
starship preset pastel-powerline -o ~/.config/starship.toml
常用模块配置¶
# 目录
[directory]
truncation_length = 3
truncation_symbol = "…/"
home_symbol = "~"
style = "bold cyan"
# Git 分支
[git_branch]
symbol = " "
style = "bold purple"
# Git 状态
[git_status]
format = '([\[$all_status$ahead_behind\]]($style) )'
style = "bold red"
conflicted = "="
ahead = "⇡${count}"
behind = "⇣${count}"
modified = "!"
untracked = "?"
staged = "+"
# Python
[python]
symbol = " "
format = '[${symbol}${pyenv_prefix}(${version} )(\($virtualenv\) )]($style)'
style = "bold yellow"
# Node.js
[nodejs]
symbol = " "
style = "bold green"
# Rust
[rust]
symbol = " "
style = "bold red"
# Docker
[docker_context]
symbol = " "
style = "bold blue"
# 命令执行时间
[cmd_duration]
min_time = 2_000 # 超过2秒才显示
format = "took [$duration](bold yellow) "
# 提示符字符
[character]
success_symbol = "[❯](bold green)"
error_symbol = "[❯](bold red)"
进阶用法¶
右侧提示符¶
right_format = """$time$battery"""
[time]
disabled = false
format = '[$time]($style) '
time_format = "%H:%M"
style = "dimmed white"
[battery]
full_symbol = "🔋"
charging_symbol = "⚡"
discharging_symbol = "💀"
[[battery.display]]
threshold = 20
style = "bold red"
自定义模块¶
# 自定义命令输出
[custom.kube]
command = "kubectl config current-context 2>/dev/null"
when = "command -v kubectl"
symbol = "☸ "
style = "bold blue"
format = '[$symbol($output )]($style)'
[custom.ip]
command = "hostname -I | awk '{print $1}'"
when = true
style = "dimmed yellow"
format = '[$output]($style) '
条件显示¶
# 只在特定目录显示
[python]
detect_extensions = ["py"]
detect_files = ["requirements.txt", "pyproject.toml", "Pipfile"]
detect_folders = [".venv", "venv"]
# 只在 SSH 时显示主机名
[hostname]
ssh_only = true
format = "[$ssh_symbol$hostname]($style) "
style = "bold dimmed green"
多行提示符¶
format = """
[┌──](bold green) $username@$hostname $directory $git_branch$git_status
[│](bold green) $python$nodejs$rust$golang
[└─](bold green) $character"""
与 Nerd Font 配合¶
[aws]
symbol = " "
[conda]
symbol = " "
[dart]
symbol = " "
[golang]
symbol = " "
[java]
symbol = " "
[lua]
symbol = " "
[nodejs]
symbol = " "
[python]
symbol = " "
[ruby]
symbol = " "
[rust]
symbol = " "
常见问题¶
Q: 图标显示为方块?¶
安装 Nerd Font 并在终端中设置使用。推荐:FiraCode Nerd Font、JetBrainsMono Nerd Font。
Q: 提示符加载慢?¶
- 调低
command_timeout - 禁用不需要的模块:
[module_name] disabled = true - 使用
starship timings诊断慢模块
Q: 如何完全隐藏某个模块?¶
Q: 与 Oh My Zsh 主题冲突?¶
Starship 替代了 Oh My Zsh 的主题功能。只需在 .zshrc 中注释掉 ZSH_THEME 设置。
参考资源¶
- 官网:https://starship.rs/
- GitHub:https://github.com/starship/starship
- 配置参考:https://starship.rs/config/
- 预设:https://starship.rs/presets/
- 模块列表:https://starship.rs/config/#prompt