Skip to content

Starship

1. Starship 是什么?

Starship 是一个跨 Shell 的命令行提示符工具。

它不是 Shell,本身不负责执行命令。它主要负责美化和增强你终端里每一行命令前面的提示符。

比如原本 Ubuntu 可能是:

shell
zhengxin@DESKTOP:~/code$

用了 Starship 后可以变成类似:

shell
zhengxin in ~/code/my-project on main via 🐍 v3.12.2 

它会根据当前目录自动显示:

当前用户
当前路径
Git 分支
Git 状态
Python 版本
Node.js 版本
命令执行耗时
上一个命令是否成功

Starship 官方定位是:fast, customizable, cross-shell prompt,也就是快速、可定制、跨 Shell 的提示符。它支持 bash、zsh、fish、PowerShell、cmd、Nushell 等多个 Shell。

2. Starship 与 Shell、zsh、fish 的关系

你可以这样理解:

shell
终端软件: WezTerm / Windows Terminal 
Shell: bash / zsh / fish / PowerShell
提示符: Starship

举例:

shell
WezTerm  是窗口
zsh/fish 是命令解释器
Starship 是命令行前面的提示符样式

所以它们不是互相替代关系。

比如你可以这样组合:

shell
WezTerm + WSL Ubuntu + zsh + Starship
WezTerm + WSL Ubuntu + fish + Starship
Windows Terminal + PowerShell + Starship

3. Starship 解决什么问题?

问题一:默认提示符信息太少

默认提示符一般只显示:

用户名
主机名
当前目录

但是开发时更关心:

现在在哪个 Git 分支?
Python 虚拟环境开了吗?
Node 版本是多少?
上一个命令有没有失败?
这个命令执行了多久?

Starship 可以自动显示这些信息。

问题二:zsh/fish/bash 配置不统一

以前如果同时用:

shell
bash、zsh、fish、PowerShell

每个 Shell 都要单独配置提示符。

Starship 的优点是:一份 starship.toml 配置,多种 Shell 共用。

它的默认配置文件是:

shell
~/.config/starship.toml

官方文档也推荐通过这个 TOML 文件统一配置。

问题三:主题折腾成本高

zsh 生态里常见方案是:

shell
oh-my-zsh
powerlevel10k
zinit
antigen

功能很强,但对新手来说配置链条比较长。

Starship 相对简单:

shell
安装 starship
 shell 配置文件里初始化
编辑 ~/.config/starship.toml

安装方式

Windows 安装

Windows 推荐用 winget:

shell
winget install --id Starship.Starship

官方文档也列出了 winget、Scoop、Chocolatey、MSI 等 Windows 安装方式。

PowerShell 中启用 Starship:

shell
notepad $PROFILE

加入:

shell
Invoke-Expression (&starship init powershell)

重启 PowerShell。

Ubuntu / WSL 安装

官方快速安装方式:

shell
curl -sS https://starship.rs/install.sh | sh

如果是 Ubuntu 25.04 及以后,也可以通过 apt 安装;但在 Ubuntu 24.04 上更常见的是官方安装脚本、Homebrew、Cargo 或手动安装。

在不同 Shell 里启用

bash

编辑:

shell
nano ~/.bashrc

加入:

shell
eval "$(starship init bash)"

fish

编辑:

shell
nano ~/.config/fish/config.fish

加入:

shell
starship init fish | source

这是官方推荐的 fish 初始化方式。

Starship 配置文件

创建配置文件:

shell
mkdir -p ~/.config
touch ~/.config/starship.toml

官方默认配置路径就是:

shell
~/.config/starship.toml

一个简单示例:

shell
# ~/.config/starship.toml

add_newline = true

format = """
$directory$git_branch$git_status$python$nodejs$cmd_duration
$character
"""

[directory]
truncation_length = 3
truncate_to_repo = true

[git_branch]
symbol = " "

[python]
symbol = "🐍 "
format = "via [$symbol$version]($style) "

[nodejs]
symbol = " "
format = "via [$symbol$version]($style) "

[cmd_duration]
min_time = 1000
format = "took [$duration]($style) "

[character]
success_symbol = "[❯](bold green)"
error_symbol = "[❯](bold red)"

效果大概是:

shell
~/code/demo on  main via 🐍 v3.12.2 via  v22.0.0 took 2s

最重要的几个配置项

format

决定提示符显示哪些模块、按什么顺序显示。

shell
format = """
$directory$git_branch$git_status$python$nodejs
$character
"""

你可以理解为:提示符总布局。

right_format

右侧提示符。

例如:

shell
format = "$directory$git_branch$character"
right_format = "$cmd_duration$time"

部分 Shell 支持右提示符,Starship 官方文档说明 right_format 可用于 fish、zsh、cmd、nushell、bash 等,不过 bash 右提示符需要额外条件。

add_newline

是否在两次提示符之间加空行。

shell
add_newline = true

想紧凑一点可以改成:

shell
add_newline = false

[directory]

控制目录显示。

shell
[directory]
truncation_length = 3
truncate_to_repo = true

这个适合项目路径很长的情况。

[character]

控制输入符号。

shell
[character]
success_symbol = "[❯](bold green)"
error_symbol = "[❯](bold red)"

成功时绿色,失败时红色。

[cmd_duration]

显示命令耗时。

shell
[cmd_duration]
min_time = 1000
format = "took [$duration]($style) "

表示命令执行超过 1 秒才显示耗时。

我的配置

你现在比较适合这种简洁开发版:

toml
# ~/.config/starship.toml
format = """
$username$directory$git_branch$python$nodejs $cmd_duration
$character"""

#right_format = "$time"
add_newline = false  # 两个命令行之间是否添加空格

[directory]
truncation_length = 3
truncate_to_repo = true
style = "bold cyan"

[git_branch]
symbol = " "
format = "on [$symbol$branch]($style) "
style = "bold purple"

[python]
symbol = "🐍 "
format = "via [$symbol$version]($style) "
style = "bold yellow"

[nodejs]
symbol = " "
format = "via [$symbol$version]($style) "
style = "bold green"

[username]
show_always = true

[hostname]
ssh_only = false

[git_status]
style = "red"

[character]
success_symbol = "[❯](green)"
error_symbol = "[❯](red)"

[time]
disabled = false
time_format = "%Y-%m-%d %H:%M:%S"
format = "$time "

一些坑

图标乱码

原因通常是终端字体没有设置 Nerd Font。

解决:

安装 Nerd Font
把 WezTerm / Windows Terminal 字体改成对应 Nerd Font
重启终端

fish 配置写成 bash 语法

fish 启用 Starship 是:

shell
starship init fish | source

不是:

shell
eval "$(starship init fish)"

Starship 是一个跨 Shell 的高性能提示符工具,适合把 bash、zsh、fish、PowerShell 的命令行体验统一起来。它不替代 Shell,只负责让你的终端提示符更美观、更有信息量。