bash - Git 分支名称不会显示在 Mac 终端上
问题描述
我在我的 Mac 中添加了以下几行~/.bash_profile
:
parse_git_branch() {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'
}
#Variables
userName="\u"
fullPath="\w"
systemName="\h"
#'$(tput setaf 14)' allows coloring for the prompt
#that immediately follows it. 14 is the color aqua.
#A list of colors can be accessed below:
#https://jonasjacek.github.io/colors/
#`$(tput sgr0)` stops the text coloring from continuing on.
#Personalized Terminal
PS1="\[$(tput setaf 118)\]${userName}\[$(tput setaf 3)\]@${systemName}\[$(tput setaf 14)\]${fullPath}\[$(tput setaf 222)\]\[$(parse_git_branch)\]\[$(tput setaf 160)\]$ \[$(tput sgr0)\]"
export PS1;
#export PS1="\u@\h \W\[\033[32m\]\$(parse_git_branch)\[\033[00m\] $ "
#------- Aliases -------
#Open Sublime Text Editor
alias subl="open -a /Applications/Sublime\ Text.app"
export ANDROID_HOME=/Users/$USER/Library/Android/sdk
export PATH=${PATH}:$ANDROID_HOME/tools:$ANDROID_HOME/platform-tools
终端样式看起来不错,但我parse_git_branch
没有显示我的 git 分支的名称。我在定义值时搞砸了PS1
,但我不确定我做错了什么。
解决方案
你可以试试这个:
PROMPT_COMMAND=$(cat << 'EOF'
git_prompt=$(git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/')
EOF
)
#Variables
userName="\u"
fullPath="\w"
systemName="\h"
PS1="\[$(tput setaf 118)\]${userName}\[$(tput setaf 3)\]@${systemName}\[$(tput setaf 14)\]${fullPath}\[$(tput setaf 222)\]\[\$git_prompt\]\[$(tput setaf 160)\]$ \[$(tput sgr0)\]"
推荐阅读
- python - Pandas / Groupby / 应用优化
- regex - 将复杂字符串通过正则表达式匹配并强制重复次数
- python - Pandas:将具有多行的 JSON 列转换为多个数据框行
- tls1.2 - 如何测试用户的浏览器是否支持某些密码并在不支持时显示友好消息
- karate - 如何在空手道中对两个数组进行一对一映射
- php - Is it possible to update the PHP version of my web template to be up to date with my hosting service?
- apache-flink - flink doesn't output anything
- node.js - Typescript + Mocha/Chai - For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves
- r - Knit Button Results Blank in R markdown
- vim - vim: is there a way to preprocess a file opened by copen