配置 bash_profile 以显示 git 分支返回我的分支两次
# show git branch
function parse_git_branch {
git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/'
}
function proml {
local BLUE="\[\033[0;34m\]"
local RED="\[\033[0;31m\]"
local LIGHT_RED="\[\033[1;31m\]"
local GREEN="\[\033[0;32m\]"
local LIGHT_GREEN="\[\033[1;32m\]"
local WHITE="\[\033[1;37m\]"
local LIGHT_GRAY="\[\033[0;37m\]"
case $TERM in
xterm*)
TITLEBAR='\[\033]0;\u@\h:\w\007\]'
;;
*)
TITLEBAR=""
;;
esac
PS1="${TITLEBAR}\
$WHITE\w$GREEN\$(parse_git_branch)$BLUE\
$GREEN\$ "
PS2='> '
PS4='+ '
}
proml
前面的代码返回分支名称三次。我只需要看一次...
~/projects/sms(apps2)$
(apps2)
(apps2)
我怎样才能纠正这个问题以仅显示路径+分支?
即.. ~/projects/sms(apps2)$
# show git branch
function parse_git_branch {
git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/'
}
function proml {
local BLUE="\[\033[0;34m\]"
local RED="\[\033[0;31m\]"
local LIGHT_RED="\[\033[1;31m\]"
local GREEN="\[\033[0;32m\]"
local LIGHT_GREEN="\[\033[1;32m\]"
local WHITE="\[\033[1;37m\]"
local LIGHT_GRAY="\[\033[0;37m\]"
case $TERM in
xterm*)
TITLEBAR='\[\033]0;\u@\h:\w\007\]'
;;
*)
TITLEBAR=""
;;
esac
PS1="${TITLEBAR}\
$WHITE\w$GREEN\$(parse_git_branch)$BLUE\
$GREEN\$ "
PS2='> '
PS4='+ '
}
proml
The previous code returns the branch name three times. I just need to see it once...
~/projects/sms(apps2)$
(apps2)
(apps2)
How can I correct this to display just the path + branch?
ie .. ~/projects/sms(apps2)$
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
脚本的最后一行把事情搞乱了。直接从
~/.bash_profile
或~/.bashrc
调用proml
仅设置一次PS*
环境变量,因此当您更改文件夹时,它不会更新(并且您稍后输入的每个文件夹中可能会显示(apps2)
)。相反,
proml
应该在脚本的最后一行设置为PROMPT_COMMAND
:PROMPT_COMMAND
保存每次在 bash 之前执行的函数的名称显示提示。有关详细信息,请参阅此处。另外,说到 git 感知的 shell 提示,对于您已有的内容来说,还有一个很好的补充。除了当前分支之外,您还可以获得有关任何未提交更改的指示。请参阅此博文了解<代码>parse_git_dirty()函数。
It's the last line of your script that messes up things. Calling
proml
directly from~/.bash_profile
or~/.bashrc
setsPS*
environment variables only once, so it won't be updated when you change folders (and you might have(apps2)
displayed in every folder you enter later).Instead,
proml
should be set asPROMPT_COMMAND
in last line of your script:PROMPT_COMMAND
holds the name of the function to be executed each time before bash displays prompt. For more info see here.Also, speaking of git-aware shell prompts, there's one nice addition to what you already have. Apart from current branch, you can get indication about any uncommitted changes. See e.g. this blog post for
parse_git_dirty()
function.尝试将最后一行更改为
PROMPT_COMMAND=proml
;-)
Try changing the last line to
PROMPT_COMMAND=proml
;-)