上一篇文章《别再社死了!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 固定格式,$output是command的输出,$style是style内容。可不写。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"