终端美化:Starship 跨 Shell 提示符的安装与配置
Starship 是一个跨 shell 的终端提示符工具——用 Rust 编写,单个二进制文件,配置文件只有一份 TOML。 支持 Bash、Zsh、Fish、PowerShell、Cmd 等主流 shell,在 Windows、macOS、Linux 上行为一致。
一、安装
Windows 推荐 winget,一行完成。macOS / Linux 用各自包管理器或直接下载二进制。
Windows(winget)
winget install --id Starship.StarshipWindows(Scoop)
scoop install starshipmacOS / Linux
brew install starshipcargo install starship --locked安装后验证:
starship --version二、PowerShell 配置
安装 Starship 后,需要在 PowerShell 的 profile 文件中添加初始化脚本。 Windows 上 PS5.1 和 PS7 的 profile 路径不同。
PS5.1 vs PS7 的 profile 路径
| 版本 | Profile 路径 |
|---|---|
| PowerShell 5.1 | $HOME\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1 |
| PowerShell 7 | $HOME\Documents\PowerShell\Microsoft.PowerShell_profile.ps1 |
2.1 添加初始化脚本
在 profile 文件中添加一行。如果 profile 文件不存在,先创建:
if (!(Test-Path $PROFILE)) { New-Item -ItemType File -Path $PROFILE -Force }notepad $PROFILEInvoke-Expression (&starship init powershell)PS7 旧版踩坑记录
Starship v1.21.x 在 PS7 上存在一个 StandardOutputEncoding 相关 bug:starship init powershell 的输出在 PS7 中可能被截断,导致提示符加载失败。
当时需要这样 workaround:
Invoke-Expression (& "C:\Program Files\starship\bin\starship.exe" init powershell --print-full-init | Out-String)此问题已在 v1.25.x 修复。如果已升级到 v1.25+,直接用 Invoke-Expression (&starship init powershell) 即可。 这个踩坑记录保留在此,以便遇到相同症状时快速定位。
2.2 使 profile 生效
. $PROFILE三、预设主题
Starship 提供多个预设主题,可以直接使用或在此基础上修改。
starship preset --liststarship preset tokyo-night -o ~/.config/starship.tomlstarship preset tokyo-night -o "$HOME\.config\starship.toml"其他预设主题
- Pastel Powerline — 彩色 Powerline 风格
- Nerd Font Symbols — 使用 Nerd Font 图标
- No Nerd Fonts — 纯文本符号,无需安装额外字体
- Bracketed Segments — 方括号分隔风格
- Plain Text — 极简纯文本
四、常用配置速查
Starship 的配置文件是 ~/.config/starship.toml(Windows 上为 %USERPROFILE%\.config\starship.toml)。 以下列出常用模块的配置片段,可直接复制后按需修改。
4.1 提示符符号
[character]success_symbol = "[>](bold green)"error_symbol = "[x](bold red)"4.2 目录显示
[directory]truncation_length = 3 # 路径截断深度truncate_to_repo = true # 在 git 仓库根目录处截断read_only = " ro" # 只读标记4.3 Git 分支与状态
[git_branch]symbol = "git "truncation_length = 20truncation_symbol = "..."[git_status]ahead = ">"behind = "<"diverged = "<>"renamed = "r"deleted = "x"4.4 语言版本显示
[nodejs]symbol = "nodejs "format = "via [$symbol($version )]($style)"[python]symbol = "py "[golang]symbol = "go "[rust]symbol = "rs "4.5 命令执行时长
[cmd_duration]min_time = 2000 # 超过 2 秒才显示show_milliseconds = true修改配置后如何生效
修改 starship.toml 后,重新打开终端或执行 . $PROFILE 即可看到效果。不需要重启 Starship 进程。
五、Claude Code 状态行集成(v1.25.0+)
Starship v1.25.0 新增了 starship statusline claude-code 子命令,提供三个 Claude Code 专用模块:
| 模块 | 显示内容 |
|---|---|
claude_model | 当前使用的模型名 |
claude_context | 上下文使用百分比 |
claude_cost | 当次会话费用估算 |
这个功能与 Claude HUD 插件的定位有重叠——HUD 是独立插件,数据更丰富(工具活动、子 Agent 状态、Todo 进度); Starship statusline 是提示符内嵌,更轻量。两者可并存,也可按需二选一。
六、验证
完成以上步骤后,逐条确认:
starship --version正常输出版本号- 打开一个新终端,提示符不再是默认的
PS C:\...>,而是 Starship 格式 $PROFILE文件中有Invoke-Expression (&starship init powershell)这一行- 进入一个 git 仓库目录,提示符显示当前分支名
- 修改
starship.toml中的success_symbol后重开终端,符号发生变化
时效性说明
⚠️ 以上信息可能已过时,请以各平台官方网站的最新公告和定价页面为准。本文基于 Starship v1.25.1 + PowerShell 7 验证,写作日期 2026-06-11。