跳转到主内容

终端美化:Starship 跨 Shell 提示符的安装与配置

2026-06-11claude-code · 终端 · Starship · PowerShell · 美化 · 配置

Starship 是一个跨 shell 的终端提示符工具——用 Rust 编写,单个二进制文件,配置文件只有一份 TOML。 支持 Bash、Zsh、Fish、PowerShell、Cmd 等主流 shell,在 Windows、macOS、Linux 上行为一致。

写作环境:Windows 11 + PowerShell 7 + Starship v1.25.1(2026-06 验证)。 不同平台安装方式不同,配置逻辑相同。

一、安装

Windows 推荐 winget,一行完成。macOS / Linux 用各自包管理器或直接下载二进制。

Windows(winget)

powershell
winget install --id Starship.Starship

Windows(Scoop)

powershell
scoop install starship

macOS / Linux

bash
# macOS (Homebrew)
brew install starship
# Linux (cargo)
cargo install starship --locked
# 或从 GitHub Releases 下载二进制:
# https://github.com/starship/starship/releases

安装后验证:

bash
starship --version
# 输出示例: starship 1.25.1

二、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 文件不存在,先创建:

powershell
# 创建 profile(如不存在)
if (!(Test-Path $PROFILE)) { New-Item -ItemType File -Path $PROFILE -Force }
# 用记事本打开编辑
notepad $PROFILE
# 添加以下内容:
Invoke-Expression (&starship init powershell)
PS7 旧版踩坑记录

Starship v1.21.x 在 PS7 上存在一个 StandardOutputEncoding 相关 bug:starship init powershell 的输出在 PS7 中可能被截断,导致提示符加载失败。

当时需要这样 workaround:

powershell
# PS7 旧版 workaround(v1.25.x 已不需要)
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 生效

powershell
# 重新加载 profile
. $PROFILE
# 或重新打开终端窗口

三、预设主题

Starship 提供多个预设主题,可以直接使用或在此基础上修改。

bash
# 查看可用预设
starship preset --list
# 应用 Tokyo Night 预设(写入 ~/.config/starship.toml)
starship preset tokyo-night -o ~/.config/starship.toml
# Windows 上等价于
starship 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 提示符符号

toml
[character]
success_symbol = "[>](bold green)"
error_symbol = "[x](bold red)"

4.2 目录显示

toml
[directory]
truncation_length = 3 # 路径截断深度
truncate_to_repo = true # 在 git 仓库根目录处截断
read_only = " ro" # 只读标记

4.3 Git 分支与状态

toml
[git_branch]
symbol = "git "
truncation_length = 20
truncation_symbol = "..."
[git_status]
ahead = ">"
behind = "<"
diverged = "<>"
renamed = "r"
deleted = "x"

4.4 语言版本显示

toml
[nodejs]
symbol = "nodejs "
format = "via [$symbol($version )]($style)"
[python]
symbol = "py "
[golang]
symbol = "go "
[rust]
symbol = "rs "

4.5 命令执行时长

toml
[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 是提示符内嵌,更轻量。两者可并存,也可按需二选一。

六、验证

完成以上步骤后,逐条确认:

  1. starship --version 正常输出版本号
  2. 打开一个新终端,提示符不再是默认的 PS C:\...>,而是 Starship 格式
  3. $PROFILE 文件中有 Invoke-Expression (&starship init powershell) 这一行
  4. 进入一个 git 仓库目录,提示符显示当前分支名
  5. 修改 starship.toml 中的 success_symbol 后重开终端,符号发生变化
时效性说明

⚠️ 以上信息可能已过时,请以各平台官方网站的最新公告和定价页面为准。本文基于 Starship v1.25.1 + PowerShell 7 验证,写作日期 2026-06-11。

本文中的大部分命令和配置都可以交给 AI 编程工具执行——粘贴到对话框让它代劳。 涉及密钥、浏览器操作或系统级修改的步骤除外。详见《从「动手做」到「指挥做」》

有疑问?来这里找答案

如果对本站内容有疑问,推荐到视频或其他知识性平台寻求解决方法,也可直接向 AI 提问获得参考性回答(注意分辨 AI 回答的正确性)

视频教程
B站搜索教程
视频演示 + 疑难解答