【终端效率工具】------ Starship:Rust 打造的极速终端提示符,配置从入门到实战
用过 oh-my-zsh + Powerlevel10k 的人都知道,好看是好看,但启动速度真的让人头疼。Starship 用 Rust 写的,跨 shell 通用,配置简单到离谱,启动速度快到飞起。这篇文章把我实际用下来的经验全倒出来,从安装到自定义主题,再到踩坑排查,照着做就行。
为什么选 Starship
先说结论:如果你想要一个启动快、配置简单、主题好看的终端提示符,Starship 是目前最优解。
对比一下常见方案:
| 方案 | 启动速度 | 配置复杂度 | 跨 Shell | 主题生态 |
|---|---|---|---|---|
| oh-my-zsh + p10k | 慢 | 高 | 仅 zsh | 丰富 |
| oh-my-zsh + spaceship | 中等 | 中等 | 仅 zsh | 一般 |
| Starship | 快 | 低 | 全部 | 内置 + 自定义 |
| 纯 zsh 原生 | 最快 | 最高 | 仅 zsh | 无 |
说白了,Starship 就是用 Rust 写了一个"万能皮肤引擎",不管你用 zsh、bash、fish 还是 PowerShell,都能用同一套配置。
安装:三行命令搞定
macOS(Homebrew)
bash
brew install starship
Linux(curl)
bash
curl -sS https://starship.rs/install.sh | sh
Windows(winget)
powershell
winget install starship
配置 Shell 集成
安装完还需要告诉你的 shell "用 Starship 作为提示符"。
zsh (~/.zshrc 末尾加):
bash
eval "$(starship init zsh)"
bash (~/.bashrc 末尾加):
bash
eval "$(starship init bash)"
fish (~/.config/fish/config.fish 末尾加):
fish
starship init fish | source
✅ 成功标志:新开终端窗口,提示符变成 Starship 默认样式。
配置文件在哪
Starship 的配置文件路径:
| 系统 | 路径 |
|---|---|
| macOS / Linux | ~/.config/starship.toml |
| Windows | C:\Users\{用户名}\.config\starship.toml |
首次使用时这个文件可能不存在,手动创建就行:
bash
mkdir -p ~/.config
touch ~/.config/starship.toml
核心概念:format 字段
Starship 的配置核心就一个字段:format。它定义了提示符长什么样、显示哪些模块、按什么顺序排列。
toml
format = """
$username\
$hostname\
$directory\
$git_branch\
$git_status\
$character"""
每个 $xxx 是一个模块(module),模块之间用 \ 连接表示同一行。想换行就加 $line_break 或者直接换行。
不写 format 会怎样
不写 format,Starship 会用默认的 $all,把所有内置模块都显示出来。好处是省事,坏处是你没法控制顺序和布局。
常用模块速查
| 模块 | 显示内容 | 触发条件 |
|---|---|---|
$os |
操作系统图标 | 默认显示 |
$username |
用户名 | 默认显示(可通过配置隐藏) |
$hostname |
主机名 | SSH 会话或配置 show_always |
$directory |
当前目录 | 默认显示 |
$git_branch |
Git 分支名 | 在 Git 仓库中 |
$git_status |
Git 状态(修改/暂存等) | 在 Git 仓库中 |
$nodejs |
Node.js 版本 | 目录下有 package.json |
$python |
Python 版本 | 目录下有 .python-version 或 pyproject.toml |
$rust |
Rust 版本 | 目录下有 Cargo.toml |
$conda |
Conda 环境名 | 激活了 Conda 环境 |
$docker_context |
Docker 上下文 | 目录下有 Dockerfile |
$cmd_duration |
上一条命令耗时 | 超过阈值 |
$time |
当前时间 | 配置启用 |
$character |
命令输入符(❯) | 默认显示 |
$line_break |
换行 | 默认启用 |
模块配置:逐个调教
每个模块都能单独配置样式、图标、显示条件。格式统一:
toml
[模块名]
disabled = false # 是否禁用
symbol = " " # 自定义图标
style = "fg:#ffffff bg:#333333" # 样式
format = '[[ $version ](fg:white)]($style)' # 自定义格式
目录模块
toml
[directory]
style = "fg:#e3e5e5 bg:#769ff0"
format = "[ $path ]($style)"
truncation_length = 3 # 路径截断层级
truncation_symbol = ".../" # 截断后的省略符号
truncate_to_repo = true # Git 仓库内只显示仓库内路径
# 路径别名替换
[directory.substitutions]
"Documents" = " "
"Downloads" = " "
"Music" = " "
Git 模块
toml
[git_branch]
symbol = " "
style = "bg:#394260"
format = '[[ $symbol $branch ](fg:#769ff0 bg:#394260)]($style)'
[git_status]
style = "bg:#394260"
format = '[[($all_status$ahead_behind )](fg:#769ff0 bg:#394260)]($style)'
$all_status 会显示这些状态标记:
| 标记 | 含义 |
|---|---|
⇡⇣ |
有提交待推送/拉取 |
⇡ |
有提交待推送 |
⇣ |
有提交待拉取 |
* |
有未暂存的修改 |
~ |
有已暂存的修改 |
+ |
有新增文件 |
>> |
有重命名文件 |
✘ |
有已删除文件 |
$ |
有隐藏的文件(stash) |
= |
有合并冲突 |
语言模块
语言模块的触发条件是目录下存在特定文件,比如 package.json 触发 Node.js,Cargo.toml 触发 Rust。
toml
[nodejs]
symbol = " "
style = "bg:#212736"
format = '[[ $symbol ($version) ](fg:#769ff0 bg:#212736)]($style)'
detect_extensions = ["js", "ts"] # 扩展名检测
detect_files = ["package.json"] # 文件检测
detect_folders = ["node_modules"] # 目录检测
[python]
symbol = " "
style = "bg:#212736"
format = '[[ $symbol ($version)(\(#$virtualenv\)) ](fg:#769ff0 bg:#212736)]($style)'
Conda 环境模块
这个坑过我。默认情况下,激活 base 环境也会显示,很烦。设 ignore_base = true 可以隐藏它:
toml
[conda]
symbol = " "
style = "fg:crust bg:sapphire"
format = '[$symbol$environment ]($style)'
ignore_base = true # 忽略 base 环境,只显示自定义环境
命令耗时模块
默认只显示超过 2 秒的命令耗时,可以调:
toml
[cmd_duration]
show_milliseconds = true
format = " in $duration "
style = "bg:lavender"
min_time = 500 # 超过 500ms 就显示
show_notifications = true # 超过阈值发系统通知
min_time_to_notify = 45000 # 超过 45 秒才发通知
时间模块
toml
[time]
disabled = false
time_format = "%R" # 只显示时:分,不显示秒
style = "bg:#1d2230"
format = '[[ $time ](fg:#a0a9cb bg:#1d2230)]($style)'
自定义配色:palette 机制
Starship 支持自定义颜色调色板,在 format 里用颜色名引用,改一处全局生效。
toml
palette = 'catppuccin_mocha' # 使用哪个调色板
[palettes.catppuccin_mocha]
rosewater = "#f5e0dc"
flamingo = "#f2cdcd"
pink = "#f5c2e7"
mauve = "#cba6f7"
red = "#f38ba8"
maroon = "#eba0ac"
peach = "#fab387"
yellow = "#f9e2af"
green = "#a6e3a1"
teal = "#94e2d5"
sky = "#89dceb"
sapphire = "#74c7ec"
blue = "#89b4fa"
lavender = "#b4befe"
text = "#cdd6f4"
subtext1 = "#bac2de"
subtext0 = "#a6adc8"
overlay2 = "#9399b2"
overlay1 = "#7f849c"
overlay0 = "#6c7086"
surface2 = "#585b70"
surface1 = "#45475a"
surface0 = "#313244"
base = "#1e1e2e"
mantle = "#181825"
crust = "#11111b"
配置好之后,模块里就能这样用:
toml
[directory]
style = "bg:peach fg:crust" # 用颜色名而不是 hex 值
可以定义多个调色板,用 palette 字段切换,不用一个个模块改颜色。
使用内置 Preset
不想从零配置?Starship 内置了几套主题,一行命令搞定���
bash
# 查看所有内置 preset
starship preset --list
# 应用 Nerd Font Symbols preset
starship preset nerd-font-symbols -o ~/.config/starship.toml
# 应用 Catppuccin Powerline(需要 Nerd Font)
starship preset catppuccin-powerline -o ~/.config/starship.toml
# 应用 Gruvbox Rainbow
starship preset gruvbox-rainbow -o ~/.config/starship.toml
# 应用 Pastel Powerline
starship preset pastel-powerline -o ~/.config/starship.toml
⚠️ 注意:-o 参数会覆盖你的现有配置。想保留自己的配置,先备份:
bash
cp ~/.config/starship.toml ~/.config/starship.toml.bak
starship preset catppuccin-powerline -o ~/.config/starship.toml
内置 Preset 列表
| Preset | 风格 | 需要 Nerd Font |
|---|---|---|
nerd-font-symbols |
Nerd Font 图标替换 | ✅ |
plain-text-symbols |
纯文本图标 | ❌ |
no-runtimes |
隐藏语言版本 | ❌ |
no-empty-icons |
隐藏无检测文件的图标 | ❌ |
pastel-powerline |
柔和色 Powerline | ✅ |
catppuccin-powerline |
Catppuccin 配色 Powerline | ✅ |
gruvbox-rainbow |
Gruvbox 彩虹色 | ✅ |
tokyo-night |
Tokyo Night 配色 | ✅ |
plain-text-symbols |
纯文本符号 | ❌ |
格式化语法:你必须知道的几个符号
Starship 的格式化语法有点像 Markdown + 模板引擎的混合体,掌握这几个就够用了:
toml
# 基本格式:[文本](样式)
format = "[ hello ](fg:#ffffff bg:#000000)"
# 模块变量
format = "[$version]($style)"
# 条件显示(括号内的内容如果变量为空就不显示)
format = "[$symbol( $version)]($style)"
# 链式样式(多个片段拼接)
format = "[ $path ](bg:blue fg:white)[ ](fg:blue bg:green)"
# 加粗、斜体
format = "[bold text](bold fg:red)"
format = "[italic text](italic fg:blue)"
样式属性
| 属性 | 示例 | 说明 |
|---|---|---|
fg |
fg:#ffffff 或 fg:red |
前景色 |
bg |
bg:#000000 或 bg:blue |
背景色 |
bold |
bold |
加粗 |
italic |
italic |
斜体 |
underline |
underline |
下划线 |
dimmed |
dimmed |
暗淡 |
可以组合:[text](bold underline fg:green bg:#000)
单行 vs 双行提示符
单行(所有内容在同一行)
toml
format = """
$os $username $directory $git_branch $git_status $nodejs $time $character"""
双行(皮肤在上面,输入在下面)
toml
format = """
$os $username $directory $git_branch $git_status $nodejs $time
$character"""
# 或者用 $line_break 控制
format = """
$os $username $directory $git_branch $git_status $nodejs $time\
$line_break\
$character"""
[line_break] 模块的 disabled 字段控制是否换行:
toml
[line_break]
disabled = false # true = 单行,false = 双行
Powerline 风格:用 Nerd Font 打造高级感
Powerline 风格的提示符需要两个东西:
- Nerd Font(带图标的字体)
- 正确的分隔符配置
安装 Nerd Font
bash
# macOS
brew install font-meslo-lg-nerd-font
# 或者
brew install font-fira-code-nerd-font
# Linux
# 去 https://www.nerdfonts.com/font-downloads 下载
然后在终端设置里把字体改成对应的 Nerd Font。
Powerline 分隔符
常用的分隔符字符(需要 Nerd Font 支持):
| 字符 | 代码 | 说明 |
|---|---|---|
| `` | |
右箭头(最常用) |
| `` | |
左箭头 |
| `` | |
细右箭头 |
| `` | |
细左箭头 |
在配置里直接粘贴字符就行:
toml
format = """
[](red)\
$os\
$username\
[](bg:peach fg:red)\
$directory\
[](fg:peach bg:yellow)\
$git_branch\
[](fg:yellow)\
$character"""
实战:我的完整配置
这是我目前在用的配置,Catppuccin Mocha 配色,双行 Powerline 风格:
toml
"$schema" = 'https://starship.rs/config-schema.json'
format = """
[](red)\
$os\
$username\
[](bg:peach fg:red)\
$directory\
[](fg:peach bg:yellow)\
$git_branch\
$git_status\
[](fg:yellow bg:green)\
$nodejs\
$python\
$rust\
[](fg:green bg:sapphire)\
$conda\
[](fg:sapphire bg:lavender)\
$time\
[](fg:lavender)\
$cmd_duration\
$line_break\
$character"""
palette = 'catppuccin_mocha'
[os]
disabled = false
style = "bg:red fg:crust"
[os.symbols]
Macos = ""
[username]
show_always = true
style_user = "bg:red fg:crust"
style_root = "bg:red fg:crust"
format = '[ $user]($style)'
[directory]
style = "bg:peach fg:crust"
format = "[ $path ]($style)"
truncation_length = 3
truncation_symbol = ".../"
[git_branch]
symbol = " "
style = "bg:yellow"
format = '[[ $symbol $branch ](fg:crust bg:yellow)]($style)'
[git_status]
style = "bg:yellow"
format = '[[($all_status$ahead_behind )](fg:crust bg:yellow)]($style)'
[nodejs]
symbol = " "
style = "bg:green"
format = '[[ $symbol( $version) ](fg:crust bg:green)]($style)'
[python]
symbol = " "
style = "bg:green"
format = '[[ $symbol( $version)(\(#$virtualenv\)) ](fg:crust bg:green)]($style)'
[conda]
symbol = " "
style = "fg:crust bg:sapphire"
format = '[$symbol$environment ]($style)'
ignore_base = true
[time]
disabled = false
time_format = "%R"
style = "bg:lavender"
format = '[[ $time ](fg:crust bg:lavender)]($style)'
[cmd_duration]
show_milliseconds = true
format = " in $duration "
style = "bg:lavender"
disabled = false
[line_break]
disabled = false
[character]
success_symbol = '[❯](bold fg:green)'
error_symbol = '[❯](bold fg:red)'
[palettes.catppuccin_mocha]
rosewater = "#f5e0dc"
flamingo = "#f2cdcd"
pink = "#f5c2e7"
mauve = "#cba6f7"
red = "#f38ba8"
maroon = "#eba0ac"
peach = "#fab387"
yellow = "#f9e2af"
green = "#a6e3a1"
teal = "#94e2d5"
sky = "#89dceb"
sapphire = "#74c7ec"
blue = "#89b4fa"
lavender = "#b4befe"
text = "#cdd6f4"
subtext1 = "#bac2de"
subtext0 = "#a6adc8"
overlay2 = "#9399b2"
overlay1 = "#7f849c"
overlay0 = "#6c7086"
surface2 = "#585b70"
surface1 = "#45475a"
surface0 = "#313244"
base = "#1e1e2e"
mantle = "#181825"
crust = "#11111b"
常见问题排查
1. 提示符没变化,还是系统默认
原因 :Shell 配置文件没加载,或者 eval "$(starship init zsh)" 放错位置。
解决 :确保这行在 ~/.zshrc(或对应 shell 的配置文件)的最后一行 。如果用了 oh-my-zsh,要放在 source $ZSH/oh-my-zsh.sh 之后。
验证:
bash
echo $STARSHIP_SHELL # 应该输出你的 shell 名称
2. 图标显示成方块或问号
原因:终端字体不支持 Nerd Font 图标。
解决 :安装 Nerd Font 并在终端设置中选择它。macOS 推荐 MesloLGS Nerd Font 或 FiraCode Nerd Font。
验证 :在终端输入 echo "",应该能看到一个 Git 分支图标,而不是方块。
3. 某个模块不显示
原因 :通常是触发条件不满足。比如 nodejs 模块只在目录下有 package.json 时才显示。
排查步骤:
bash
# 检查模块是否被禁用
grep "disabled" ~/.config/starship.toml
# 检查触发文件是否存在
ls package.json # 对于 nodejs
ls Cargo.toml # 对于 rust
ls pyproject.toml # 对于 python
4. Conda 环境名不显示
原因 :conda 模块不在 format 字段里。
解决 :确保 $conda 出现在 format 字符串中。
5. 颜色显示不正确
原因:终端不支持 true color(24 位色)。
解决:在终端设置里开启 true color 支持。iTerm2 默认支持,macOS 自带 Terminal.app 不支持(建议换 iTerm2 或 WezTerm)。
6. Powerline 分隔符有间隙
原因:终端的行高或字距设置导致分隔符之间有空隙。
解决:在终端设置里���行高(Line Height)设为 100%,字距(Letter Spacing)设为 0。
7. 配置修改后不生效
原因:Starship 会缓存配置。
解决:
bash
# 重新加载配置
source ~/.zshrc
# 或者直接开新终端窗口
调试技巧
查看当前配置
bash
starship config # 输出当前生效的完整配置
测试提示符渲染
bash
# 在当前目录测试提示符输出
STARSHIP_CONFIG=~/.config/starship.toml starship prompt
找到拖慢速度的模块
bash
# 显示每个模块的渲染耗时
starship timings
如果有模块耗时特别高,考虑禁用它或者调大 command_timeout。
总结:你真正需要记住的事
- 配置文件 :
~/.config/starship.toml,不存在就手动创建。 - 核心字段 :
format决定显示什么、按什么顺序。 - 模块触发:语言模块只在检测到对应文件时才显示,别以为配置坏了。
- Nerd Font:想用好看的图标必须装 Nerd Font,否则都是方块。
- Conda 坑 :
$conda要手动加到format里,ignore_base = true省心。 - Presets :不想从零配置就用
starship preset一键套主题。 - 调试三件套 :
starship config、starship prompt、starship timings。
验证清单
- ⭐ Starship 已安装(
starship --version能输出版本号) - ⭐ Shell 配置文件末尾有
eval "$(starship init xxx)" - ⭐ Nerd Font 已安装并在终端中启用
- 配置文件
~/.config/starship.toml存在 -
$conda模块已加入format(如果用 Conda) -
$character模块在format末尾(否则没有输入提示符) - 开新终端窗口,提示符显示正常
- 在 Git 仓库目录下,分支名和状态能正确显示
- 图标正常显示,没有方块或问号