作为一名程序员,终端是日常工作中最频繁使用的工具之一。一个美观、高效的终端环境不仅能提升工作效率,还能让编程体验更加愉悦。本文将介绍如何使用 Starship 打造一个现代化、跨平台的终端提示符。
什么是 Starship?
Starship 是一个轻量、快速、跨 shell 的现代化提示符工具:
- 跨平台:支持 macOS、Linux、Windows
- 跨 Shell:支持 Bash、Zsh、Fish、PowerShell 等
- 高度可定制:丰富的配置选项
- 性能优异:使用 Rust 编写,启动速度极快
安装前置条件
安装一个 Nerd Font 字体,否则图标可能显示为乱码:
1 2 3
| brew tap homebrew/cask-fonts brew install --cask font-meslo-lg-nerd-font
|
安装后在终端设置中将字体改为 MesloLGS NF。
安装 Starship
1 2 3 4 5
| brew install starship
curl -sS https://starship.rs/install.sh | sh
|
配置 Shell
根据你使用的 Shell 添加初始化命令:
1 2 3 4 5 6 7
| echo 'eval "$(starship init zsh)"' >> ~/.zshrc source ~/.zshrc
echo 'eval "$(starship init bash)"' >> ~/.bashrc source ~/.bashrc
|
创建配置文件
1 2
| mkdir -p ~/.config touch ~/.config/starship.toml
|
推荐配置(青色背景风格)
一个简洁美观的配置效果:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
|
format = """[$username@$hostname$directory$git_branch$git_status](bg:cyan fg:black) $character""" add_newline = false
[username] show_always = true format = "$user"
[hostname] ssh_only = false format = "$hostname"
[directory] format = ">$path" truncation_length = 3
[git_branch] format = ">$branch"
[git_status] format = "$all_status"
[character] success_symbol = "[❯](bold cyan)" error_symbol = "[❯](bold red)"
|
效果展示:
1
| foleydang@hostname>workspace>master ❯
|
Powerline 风格配置
如果你喜欢更丰富的效果:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
| format = """ [](#9A348E)$directory\ [](fg:#9A348E bg:#DA627D)$git_branch$git_status\ [](fg:#DA627D bg:#FCA17D)$nodejs$python$rust\ [](fg:#FCA17D bg:#86BBD8)$time\ [ ](fg:#86BBD8)\ $line_break$character"""
[directory] style = "bg:#9A348E" truncation_length = 3 truncation_symbol = "…/"
[git_branch] symbol = " " style = "bg:#DA627D" format = '[$symbol$branch ]($style)'
[git_status] style = "bg:#DA627D" format = '[$all_status$ahead_behind ]($style)'
[nodejs] symbol = " " style = "bg:#FCA17D" format = '[$symbol($version )]($style)'
[python] symbol = " " style = "bg:#FCA17D" format = '[$symbol($version )]($style)'
[time] disabled = false time_format = "%R" style = "bg:#86BBD8" format = '[$time ]($style)'
[character] success_symbol = "[➜](bold green)" error_symbol = "[➜](bold red)"
|
一键安装脚本
保存为 install-starship.sh:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
| #!/bin/bash
if command -v brew &> /dev/null; then brew install starship elif command -v curl &> /dev/null; then curl -sS https://starship.rs/install.sh | sh else echo "需要 Homebrew 或 curl 来安装 Starship" exit 1 fi
mkdir -p ~/.config cat > ~/.config/starship.toml << 'STARSHIP_EOF' format = """[$username@$hostname$directory$git_branch$git_status](bg:cyan fg:black) $character""" add_newline = false
[username] show_always = true format = "$user"
[hostname] ssh_only = false format = "$hostname"
[directory] format = ">$path" truncation_length = 3
[git_branch] format = ">$branch"
[git_status] format = "$all_status"
[character] success_symbol = "[❯](bold cyan)" error_symbol = "[❯](bold red)" STARSHIP_EOF
if [ -f ~/.zshrc ]; then grep -q "starship init zsh" ~/.zshrc || echo 'eval "$(starship init zsh)"' >> ~/.zshrc echo "✅ 已添加到 ~/.zshrc" elif [ -f ~/.bashrc ]; then grep -q "starship init bash" ~/.bashrc || echo 'eval "$(starship init bash)"' >> ~/.bashrc echo "✅ 已添加到 ~/.bashrc" fi
echo "✅ Starship 配置完成" echo "运行 source ~/.zshrc 或 source ~/.bashrc 使配置生效"
|
运行:
1 2
| chmod +x install-starship.sh ./install-starship.sh
|
远程主机迁移
复制配置到远程主机
1 2 3 4 5 6
| scp ~/.config/starship.toml user@remote:~/.config/
ssh user@remote "curl -sS https://starship.rs/install.sh | sh" ssh user@remote "echo 'eval \"\$(starship init zsh)\"' >> ~/.zshrc"
|
使用一键脚本
1
| scp install-starship.sh user@remote:~/ssh user@remote "chmod +x ~/install-starship.sh && ~/install-starship.sh"
|
自定义颜色
修改 bg:cyan fg:black 可以更换配色:
可用颜色:
black, white, red, green, yellow, blue, magenta, cyan
- 或使用 hex:
bg:#1e1e2e fg:#cdd6f4
示例(紫色背景):
1
| format = """[$username@$hostname$directory](bg:magenta fg:white) $character"""
|
配合 iTerm2 使用
进一步优化终端体验:
- 设置字体:Preferences → Profiles → Text → Font → MesloLGS NF
- 启用真彩色:Preferences → Profiles → Terminal → Enable True Color
- 推荐配色:Dracula 或 Snazzy 主题
常见问题
图标显示为乱码
确保终端字体设置为 Nerd Font。
启动速度慢
减少不必要的模块可以提升启动速度。
颜色不显示
- 不要手动设置 TERM 环境变量
- 某些终端(如 basic TUI)不支持颜色
配置文件位置
| 文件 |
位置 |
| Starship 配置 |
~/.config/starship.toml |
| Zsh 初始化 |
~/.zshrc |
| Bash 初始化 |
~/.bashrc |
总结
Starship 让终端焕然一新,推荐配置组合:
- 终端:iTerm2
- Shell:Zsh
- 提示符:Starship
- 字体:MesloLGS NF
- 配色:青色背景 + 黑色字体
Happy Coding!
参考:Starship 官方文档