-
Notifications
You must be signed in to change notification settings - Fork 2
Open
Labels
Description
简单定制 oh-my-zsh 提示信息
背景
有两套git
配置,包括名称、邮箱等,平日使用git config
对不同的代码仓库进行不同的设置,但是时间久了很容易忘记哪个仓库对应哪套配置,可能导致提交的内容使用了错误的配置,因此,希望命令行进入到对应的目录后,有相应的标识
解决方案
这边一直使用的是zsh
+oh-my-zsh
,bira
主题,因此希望通过扩展主题的方式实现想要的效果
主题样式如下:
╭─yangruihan@MBP ~/xxx <master>
╰─$
希望实现的效果如下:
╭─yangruihan@MBP ~/xxx [github] <master>
╰─$
其中[github]
表示当前使用的是 github 对应的配置
1 准备主题配置
将当前使用的配置复制一份进行修改
$ cp ~/.oh-my-zsh/themes/bira.zsh-theme ~/.oh-my-zsh/themes/bira2.zsh-theme
2 修改配置内容
打开bira2.zsh-theme
在其中插入如下代码:
# 判断当前使用git配置
function get_git_acc_hint() {
# 如果当前没有在git仓库目录下,则不显示内容
if [ "$(git_prompt_info)" = "" ]; then
return
fi
# 得到当前git配置的邮箱
local git_email=`git config user.email`
# 根据邮箱显示不同的内容
if [ "${git_email}" = "yangruihan@vip.qq.com" ]; then
echo '[Github] '
else
echo '[OtherGit] '
fi
}
其中$(git_prompt_info)
是oh-my-zsh
自带的git
库里的方法,源码在这里
接着,添加一个变量:
local git_acc_hint='$(get_git_acc_hint)'
最后修改提示变量内容:
PROMPT="╭─${user_host}${current_dir}${git_acc_hint}${rvm_ruby}${git_branch}${venv_prompt}
╰─%B${user_symbol}%b "
3 修改配置
编辑~/.zshrc
将其中主题配置改成bira2
:
ZSH_THEME="bira2"
到这里就大功告成了,回到命令行使用source ~/.zshrc
立马就可以看到效果了
完整配置内容
# ZSH Theme - Preview: https://gyazo.com/8becc8a7ed5ab54a0262a470555c3eed.png
local return_code="%(?..%{$fg[red]%}%? ↵%{$reset_color%})"
if [[ $UID -eq 0 ]]; then
local user_host='%{$terminfo[bold]$fg[red]%}%n@%m %{$reset_color%}'
local user_symbol='#'
else
local user_host='%{$terminfo[bold]$fg[green]%}%n@%m %{$reset_color%}'
local user_symbol='$'
fi
local current_dir='%{$terminfo[bold]$fg[blue]%}%~ %{$reset_color%}'
local git_branch='$(git_prompt_info)'
local rvm_ruby='$(ruby_prompt_info)'
local venv_prompt='$(virtualenv_prompt_info)'
ZSH_THEME_RVM_PROMPT_OPTIONS="i v g"
# 判断当前使用git配置
function get_git_acc_hint() {
# 如果当前没有在git仓库目录下,则不显示内容
if [ "$(git_prompt_info)" = "" ]; then
return
fi
# 得到当前git配置的邮箱
local git_email=`git config user.email`
# 根据邮箱显示不同的内容
if [ "${git_email}" = "yangruihan@vip.qq.com" ]; then
echo '[Github] '
else
echo '[OtherGit] '
fi
}
local git_acc_hint='$(get_git_acc_hint)'
PROMPT="╭─${user_host}${current_dir}${git_acc_hint}${rvm_ruby}${git_branch}${venv_prompt}
╰─%B${user_symbol}%b "
RPROMPT="%B${return_code}%b"
ZSH_THEME_GIT_PROMPT_PREFIX="%{$fg[yellow]%}‹"
ZSH_THEME_GIT_PROMPT_SUFFIX="› %{$reset_color%}"
ZSH_THEME_RUBY_PROMPT_PREFIX="%{$fg[red]%}‹"
ZSH_THEME_RUBY_PROMPT_SUFFIX="› %{$reset_color%}"
ZSH_THEME_VIRTUAL_ENV_PROMPT_PREFIX="%{$fg[green]%}‹"
ZSH_THEME_VIRTUAL_ENV_PROMPT_SUFFIX="› %{$reset_color%}"
ZSH_THEME_VIRTUALENV_PREFIX=$ZSH_THEME_VIRTUAL_ENV_PROMPT_PREFIX
ZSH_THEME_VIRTUALENV_SUFFIX=$ZSH_THEME_VIRTUAL_ENV_PROMPT_SUFFIX