使用 Starship 配置跨平台 prompt
Starship 是跨平台 prompt,可以在 Linux、macOS、Windows 的 zsh / bash / fish /
PowerShell 里共用一套 starship.toml。PowerShell 版本、Terminal / VS Code 字体、启动优化、
模块和 alias 放在 PowerShell profile,这里只记录
Starship 自己的安装、初始化和配置。
安装 Starship
Linux
curl -sS https://starship.rs/install.sh | shmacOS
brew install starshipWindows
winget install --id Starship.Starship如果更想用 Scoop,也可以直接安装。
scoop install starship配置 shell 初始化
zsh
把 Starship 初始化放到 ~/.zshrc 的末尾,避免被其他 prompt 覆盖。
echo 'eval "$(starship init zsh)"' >> ~/.zshrc如果之前启用了 zsh framework 自带主题、powerlevel10k 或其他 prompt,需要先注释掉旧主题, 避免两个 prompt 同时初始化。
如果使用 zimfw,也要检查 ~/.zimrc。不要同时加载 powerlevel10k、zimfw theme module
或其他 prompt / theme module。如果已经用 zmodule joke/zim-starship,Starship 会由 zimfw
module 初始化,不需要再把 eval "$(starship init zsh)" 写进 ~/.zshrc。改完 ~/.zimrc
后运行 zimfw install,再重新打开 shell。
Zim install 默认生成的 Prompt 段也要保持注释,尤其不要打开 zmodule asciiship。如果
duration-info、git-info、asciiship 被启用,说明 Zim 的 prompt 分支也参与初始化了。
PowerShell
# 使用记事本打开 powershell 的配置文件
# if the profile file does not exist, create it
if (-not (Test-Path $profile)) { New-Item $profile -Force }
# Open the profile file in notepad
notepad $PROFILE在配置文件内添加一行,让 PowerShell 每次打开都自动加载 Starship。
Invoke-Expression (&starship init powershell)如果之前启用了 oh-my-posh,需要把下面这种初始化行注释掉或者删掉。
# oh-my-posh init pwsh --config "$env:POSH_THEMES_PATH/powerlevel10k_rainbow.omp.json" | Invoke-Expression配置 Starship
Starship 默认读取 $HOME/.config/starship.toml。Linux / macOS 可以这样创建配置文件。
mkdir -p ~/.config
${EDITOR:-vi} ~/.config/starship.tomlWindows PowerShell 也使用同一个路径。
if (-not (Test-Path "$HOME\.config")) { New-Item -ItemType Directory "$HOME\.config" }
if (-not (Test-Path "$HOME\.config\starship.toml")) {
New-Item -ItemType File "$HOME\.config\starship.toml"
}
notepad "$HOME\.config\starship.toml"一个短配置示例,减少 ANSI 背景色和 powerline 分隔符导致的宽度误判,也方便三端共用。
"$schema" = 'https://starship.rs/config-schema.json'
format = "$directory$git_branch$git_status$nodejs$python$rust$bun$character"
add_newline = false
scan_timeout = 10
command_timeout = 300
# =========================================================
# DIRECTORY
# =========================================================
[directory]
style = "fg:67"
format = '[\[$path\]]($style)'
truncation_length = 3
truncation_symbol = "../"
[directory.substitutions]
"Documents" = "Docs"
"Downloads" = "Down"
"Music" = "Music"
"Pictures" = "Pics"
# =========================================================
# GIT
# =========================================================
[git_branch]
symbol = "🌱 "
style = "fg:107"
format = '[\[$symbol$branch\]]($style)'
[git_status]
style = "fg:107"
format = '([\[$all_status$ahead_behind\]]($style))'
conflicted = "="
ahead = "⇡${count}"
diverged = "⇕⇡${ahead_count}⇣${behind_count}"
behind = "⇣${count}"
up_to_date = ""
untracked = "?"
stashed = '\$'
modified = "!"
staged = "+"
renamed = "»"
deleted = "x"
# =========================================================
# LANGUAGES
# =========================================================
[nodejs]
symbol = "[⬢](bold green) "
style = "fg:67"
format = '[\[$symbol$version\]]($style)'
[python]
symbol = "🐍 "
style = "fg:67"
format = '[\[$symbol$version\]]($style)'
[rust]
symbol = "🦀 "
style = "fg:67"
format = '[\[$symbol$version\]]($style)'
[bun]
symbol = "🥟 "
style = "fg:67"
format = '[\[$symbol$version\]]($style)'
# =========================================================
# CHARACTER
# =========================================================
[character]
success_symbol = '[>](fg:67)'
error_symbol = '[>](fg:167)'
vimcmd_symbol = '[<](fg:107)'更多配置可以看官方配置文档 。