我的 starship 终端配置

上一篇文章《别再社死了!includeIf 一招搞定 Git 提交者信息错乱,守护你的邮箱隐私》我们通过巧妙的 git 配置让进入不同目录能自动切换对应的用户名和邮箱,从根源上杜绝了 git 提交作者混乱问题。

今天我们直接让 git 用户名和邮箱在终端显示,更加一目了然,配置效果大概如下:

ts 复制代码
❯ nextjs-ecommerce on 🌱 master [⇡] via Node.js v24.9.0 👨 legend80s 💌 legend80s@example.com

如果你按照之前的文章配置过 git 自动切换用户名,现在切换到工作目录可以看到用户名邮箱也随着改变并直观展示在终端上。非常 NICE!

安装 starship

详见官网 starship.rs/ 略。

配置

完整配置如下:

toml 复制代码
# ~/.config/starship.toml
# https://starship.rs/config/

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

# Inserts a blank line between shell prompts
add_newline = true

[git_branch]
symbol = '🌱 '

[nodejs]
format = 'via [Node.js $version](bold green) '

[custom.git_user]
command = "echo \"👨 $(git config user.name) 💌 $(git config user.email)\""
shell = ["bash", "--noprofile", "--norc"]
style = "bold yellow"
when = "git rev-parse --is-inside-work-tree 2>/dev/null"

解释

  • "$schema" = 'https://starship.rs/config-schema.json' 是为了让接下来的配置能自动补全。
  • add_newline = true 前后命令行之间增加空行,给予呼吸空间。
  • [git_branch] symbol = '🌱 给分支增加 emoji,可以换成任意你喜欢的 emoji。如果不改必须安装 nerd 字体,否则显示乱码。
  • format = 'via [Node.js $version](bold green) ' 绿色加粗显示 Node.js 版本号:Via Node.js v24.x.x 。你可以改成任意代表 Node.js 的 emoji 比如 🟢。如果不改必须安装 nerd 字体,否则显示乱码。
    • 简洁配置:format = '[🟢 $version](bold green) '。效果: nextjs-ecommerce on 🌱 master [⇡] 🟢 v24.9.0

接下来是配置 git 用户名和邮箱,也是本文的重点。

显示 git user 和 email

toml 复制代码
[custom.git_user]
command = "echo \"👨 $(git config user.name) 💌 $(git config user.email)\""
shell = ["bash", "--noprofile", "--norc"]
style = "bold yellow"
# format = "[$output]($style) "
when = "git rev-parse --is-inside-work-tree 2>/dev/null"
解释

custom 表示自定义配置

  • command 表示 starship 将执行该命令来展示信息。我们配置echo \"👨 $(git config user.name) 💌 $(git config user.email)\"让其显示美观的:👨 username 💌 foo@example.com
  • shell:采用何种解释器运行 command。指定 bash,为了兼容性需指定否则 trae 中将不展示,因为 trae 默认使用 PowerShell。
  • format:starship 固定格式,$outputcommand 的输出,$stylestyle 内容。可不写
  • style:加粗黄色,因为这个信息比较重要,当然你可以换成其他颜色,比如 red 或 purple,或使用其他样式比如 underline。更多详见参考链接。
  • when = "...":何时展示,"true" 则始终展示。git rev-parse --is-inside-work-tree 2>/dev/null 只在Git 仓库目录内(git init 过) 才显示这个,2>/dev/null 表示忽略报错,报错不输出 true 自然也不展示。

最终效果

如果大家觉得 Node.js 版本号和用户名之间空白太多,可以使用 \b 回退符。

diff 复制代码
+ command = "echo \"\b👨 $(git config user.name) 💌 $(git config user.email)\""

如果在小屏幕上可改成只显示用户名(要是能自适应就好了,但确实没有找到办法 🤷‍♂️):

diff 复制代码
+ command = "echo \"\b👨 $(git config user.name)\""

对应配置:

toml 复制代码
# ~/.config/starship.toml
# https://starship.rs/config/

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

# Inserts a blank line between shell prompts
add_newline = true

[git_branch]
symbol = '🌱 '

[nodejs]
format = '[🟢 $version](bold green) '

[custom.git_user]
command = "echo \"\b👨 $(git config user.name) 💌 $(git config user.email)\""
style = "bold yellow"
when = "git rev-parse --is-inside-work-tree 2>/dev/null"

注意修改实时生效无需重启终端,只需终端执行任何命令或者按 Enter 即可。

展示 npm registry

diff 复制代码
+ [custom.npm]
+ command = "registry=$(npm config get registry) && echo \"${registry#*://}\""
# when package.json exists
+ when = "[[ -f package.json ]]"

这里将 https://registry.npmjs.org 截短为 registry.npmjs.org,好让空间占据变小,当然你可以不截断:command = "npm config get registry"

其次只有当当前目录存在 package.json [[ -f package.json ]] 即是前端项目才展示,因为其他项目比如 Python 或 Golang 项目我们并不在意当前 npm 的 registry 是什么。如果多文件可用 detect_files = ["package.json"]

!TIPS\] 单文件为何推荐 `[[ -f package.json ]]` 首先 `[ -f package.json ]` 会唤起外部命令即新进程,性能慢。detect_files 不知其实现。 *`[` 是命令: 文件位置 `/usr/bin/[` 这个命令很特殊, 结尾只能是 `]` , 所以感觉像逻辑判断一样* 。 *`❯ which [` 将输出 **\[: shell built-in command** 说明它其实就是一个命令。* *英文介绍 `[`: `[ arg... ]` Evaluate conditional expression. This is a **synonym for the "test"** builtin, but the last argument **must be a literal '\]'**, to match the opening '\['.* *`test` 也是命令: `/usr/bin/test`* *`[[` 才是 bash 真正的判断逻辑* 来自:**\[ 和 \[\[ 和 test 谁更快??** - [cloud.tencent.com/developer/a...](https://link.juejin.cn?target=http%3A%2F%2Fcloud.tencent.com%2Fdeveloper%2Farticle%2F1806703 "http://cloud.tencent.com/developer/article/1806703") 更多区别:[segmentfault.com/a/119000002...](https://link.juejin.cn?target=https%3A%2F%2Fsegmentfault.com%2Fa%2F1190000022265700%23item-4-3 "https://segmentfault.com/a/1190000022265700#item-4-3")

效果

sh 复制代码
nextjs-ecommerce on 🌱 master [!⇡] 🟢 v22.19.0 👨 legend80s registry.npmjs.org took 5s
❯ npm config set registry https://registry.npmmirror.com # 切换

nextjs-ecommerce on 🌱 master [!⇡] 🟢 v22.19.0 👨 legend80s registry.npmmirror.com

可以看到当切换 registry 后相应也发生了变化 🎉!

进入一个非前端项目不展示 registry。

注意 1

diff 复制代码
# 如果超时可加
+ command_timeout = 1500

注意 2 :在 Windows 上如果发现终端enter后较慢建议不展示 npm registry。而采用下述 gituser alias 按需查看。

gituser alias

最后再贡献一个 gituser alias 如果大家不喜欢在终端时刻展示自己的用户名邮箱,可以通过该命令迅速查看。

效果如下:

ts 复制代码
 nextjs-ecommerce on 🌱 master [⇡] 🟢 v24.9.0  👨 legend80s 💌 liuchzong@qq.com 
❯ gituser

┌────────────────┬────────────────────────────────┐
│ 👨 legend80s   │ 💌 liuchzong@qq.com           │
└────────────────┴────────────────────────────────┘

~/.zshrc 文件中增加以下 function 和 alias(function 也是一种 alias 可全局调用):

sh 复制代码
// ~/.zshrc

function git-user() {
  echo
  echo ┌────────────────┬────────────────────────────────┐
  echo │ "👨 $(git config user.name)   │ 💌 $(git config user.email)            │"
  echo └────────────────┴────────────────────────────────┘
  echo
}
alias gituser="git-user"
alias current-git="git-user"

参考链接

相关推荐
wa的一声哭了1 小时前
WeBASE管理平台部署-WeBASE-Web
linux·前端·网络·arm开发·spring boot·架构·区块链
Moment1 小时前
专为 LLM 设计的数据格式 TOON,可节省 60% Token
前端·javascript·后端
JarvanMo1 小时前
Apple更新App审核条款,严打擅自与第三方 AI 共享个人数据的应用
前端
掘金安东尼1 小时前
🧭 前端周刊第440期(2025年11月10日–11月16日)
前端
青梅主码1 小时前
麦肯锡联合QuantumBlack最新发布《2025年人工智能的现状:智能体、创新和转型》报告:32% 的企业预计会继续裁员
前端·人工智能·后端
G***66912 小时前
前端性能优化插件,CSS与JavaScript压缩插件实战指南
前端·javascript·css
百花~2 小时前
Spring Web MVC~
前端·spring·mvc
fruge2 小时前
大流量场景踩坑:前端如何应对秒杀活动的并发请求
前端
IT_陈寒2 小时前
Vue 3.4 性能优化实战:7个被低估的Composition API技巧让你的应用提速30%
前端·人工智能·后端