如何在 macOS/Linux 安装 Starship 终端提示符:配置 starship.toml 并接入 zsh 与 fish

安装 Starship,配置 starship.toml,并在 zsh 或 fish 里启用跨 Shell 提示符。

终端里要同时看清Git 分支、目录深度和命令耗时,默认 PS1 往往信息不足或配色杂乱。Starship 是 Rust 写的跨 Shell 提示符,一条命令接入 zsh 或 fish,配置集中在 ~/.config/starship.toml。本教程走 macOS 与 Linux 的官方安装路径,并演示首次定制,面向 hereink 读者常用的本地开发环境。

检索词是如何在 macOS/Linux 安装 Starship 并配置 zsh 或 fish。读完应能安装二进制、写入 init 行、编辑 starship.toml,并在新终端看到带 Git 与耗时的提示符。

安装brew · install.sh · cargo
配置~/.config/starship.toml
Shellzsh · fish · bash

Starship 解决什么问题

比手写 PS1 可维护

传统 PS1 转义序列难读、难共享。Starship 用 TOML 描述模块(directory、git_branch、cmd_duration),改主题不必碰 Shell 脚本。

跨 Shell 同一套外观

在 macOS 用 zsh、远程机用 bash、本机 fish 时,同一份 starship.toml 可保持提示符一致,减少上下文切换成本。

官方文档与配置说明:starship.rs。安装后运行 starship --version 确认版本;配置 schema 见文档 Configuration 章节。

在 macOS 与 Linux 安装

  1. 1
    macOS(Homebrew,推荐)

    执行 brew install starship。验证:which starship 与 starship --version。Apple Silicon 与 Intel 共用同一 formula。

  2. 2
    Linux 发行版包

    Debian/Ubuntu 可先添加官方仓库再 apt install starship(见 starship.rs 安装页);Fedora 可用 dnf install starship;Arch 用户常用 pacman -S starship 或 AUR。

  3. 3
    通用安装脚本

    无包管理器时:curl -sS https://starship.rs/install.sh | sh。脚本会提示安装路径,通常需把 ~/.local/bin 加入 PATH。

  4. 4
    可选:cargo 安装

    已装 Rust 工具链:cargo install starship --locked。编译较慢,适合无法使用预编译包的环境。

接入 zsh 或 fish

# zsh — 追加到 ~/.zshrc
eval "$(starship init zsh)"

# fish — 追加到 ~/.config/fish/config.fish
starship init fish | source

# bash — 追加到 ~/.bashrc(若使用 bash)
eval "$(starship init bash)"

保存后新开终端或 source 对应配置文件。若提示符未变,检查 starship 是否在 PATH 中(echo $PATH)。

编辑 ~/.config/starship.toml

模块作用示例片段
[directory]当前路径,可截断长路径truncation_length = 3
[git_branch]显示 Git 分支与状态symbol = " "
[cmd_duration]慢命令耗时min_time = 500(毫秒)
format全局模块顺序format = "$directory$git_branch$character"
适合
日常在 zsh/fish 里写代码、频繁切换 Git 仓库、希望提示符轻量且可版本控制的开发者。
不适合
必须严格兼容旧式 PS1 的企业跳板机;或终端无法装 Nerd Font 且不接受纯文本符号的场景。
安装后提示 command not found: starship?

二进制不在 PATH。Homebrew 用户确认 eval "$(/opt/homebrew/bin/brew shellenv)"(Apple Silicon)或检查 /usr/local/bin。install.sh 用户把 ~/.local/bin 写入 .zshrc 的 export PATH。

和 Oh My Zsh 主题冲突吗?

启用 Starship 后应关闭或注释 OMZ 的 ZSH_THEME(设为 random 或空),只保留 starship init 一行,否则两套提示符可能叠加。

配置改了不生效?

确认编辑的是 ~/.config/starship.toml(不是项目目录里的副本)。TOML 语法错误时 Starship 会回退默认;可用 starship config 打开编辑器或查文档 Troubleshooting。

终端配好后,对齐一张架构图

本地环境顺手之后,服务边界仍适合摊在同一张白板上。短会不必新工具栈,见 如何创建空间。

即刻开会