像专业人士一样定制 MacOS 终端的 Starship

发布: (2026年1月18日 GMT+8 12:50)
3 min read
原文: Dev.to

Source: Dev.to

安装 Starship

brew install starship

编辑你的 Zsh 配置文件:

nano ~/.zshrc

在每个 Zsh 会话中加载 Starship,添加以下行:

eval "$(starship init zsh)"

可选插件

这些插件通过实时高亮命令并根据你的历史记录和当前输入提供建议,提升终端体验。

brew install zsh-syntax-highlighting
brew install zsh-autosuggestions

再次编辑 ~/.zshrc,并加入插件加载行:

source $(brew --prefix)/share/zsh-autosuggestions/zsh-autosuggestions.zsh
source $(brew --prefix)/share/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh

安装 Nerd Font

为确保终端能够正确渲染图标和自定义字形,安装 Nerd Font:

brew search font-fira-code-nerd-font   # 验证是否可用
brew install --cask font-fira-code-nerd-font

配置终端使用 Nerd Font

选项
终端字体FiraCode Nerd Font

VS Code 设置(可选)

{
  "terminal.integrated.fontFamily": "FiraCode Nerd Font",
  "editor.fontFamily": "Menlo, Monaco, 'Courier New', monospace, 'FiraCode Nerd Font'"
}

创建 Starship 配置

mkdir -p ~/.config && nano ~/.config/starship.toml

starship.toml 中添加以下内容:

# Get editor completions based on the config schema
"$schema" = 'https://starship.rs/config-schema.json'

format = """
$directory$git_branch$git_status$fill$aws$nodejs$java$gradle
$os$character
"""

[fill]
symbol = ' '

[directory]
format = '[ $path ]($style)[$read_only]($read_only_style)'
style = 'bg:blue'
read_only_style = 'bg:red'
truncate_to_repo = true
truncation_length = 1

[git_branch]
format = '[ $symbol$branch ]($style)'
style = 'bg:green'

[git_status]
format = '[$all_status$ahead_behind](bg:green)'
conflicted = '[ = ](bg:yellow bold)'
ahead = '[ ⇡ ](bg:yellow bold)'
behind = '[ ⇣ ](bg:yellow bold)'
diverged = '[ ⇕ ](bg:yellow bold)'
up_to_date = ''
untracked = '[ ? ](bg:yellow bold)'
stashed = '[ $ ](bg:yellow bold)'
modified = '[ ! ](bg:yellow bold)'
staged = '[ + ](bg:yellow bold)'
renamed = '[ » ](bg:yellow bold)'
deleted = '[ ✘ ](bg:yellow bold)'
typechanged = ""

[aws]
format = '[ $symbol $profile $region ]($style)'
symbol = ' '

[nodejs]
format = '[ $symbol $version ]($style)'
version_format = '${raw}'

[java]
format = '[ $symbol:$version ]($style)'
version_format = '${raw}'
style = 'bg:red bold'
symbol = 'jdk'

[gradle]
format = '[ $symbol $version ]($style)'
version_format = '${raw}'
symbol = ''

[os]
format = '[$symbol ]($style)'
disabled = false

[os.symbols]
Macos = '󰀵'

[character]
success_symbol = "[❯](bold default)"
error_symbol = '[✗](bold red) '

使用提示符

AWS 配置文件

通过设置 AWS_PROFILE 环境变量来切换 AWS 配置文件:

export AWS_PROFILE=your-profile-name

提示符会显示当前的 AWS 配置文件(例如 root)和区域(例如 us-east-1)。

Node.js

在 Node.js 项目目录下打开终端。Starship 将显示当前的 Node.js 版本,并在你使用 nvm 等工具切换版本时自动更新:

nvm use 18

Git 信息(分支、状态图标)也会一起显示。

Java / Gradle

在 Java 项目目录下打开终端。提示符将显示:

  • 当前的 Java 版本。
  • 当前的 Gradle 版本。
  • 任意已启用的 AWS 配置文件和区域信息。

尽情享受你定制的、信息丰富的终端吧!

Back to Blog

相关文章

阅读更多 »