如何在Bash提示符中显示带颜色的git分支?
我在git存储库中使用本指南在gnome终端(Ubuntu 15.10)中显示分支名称。 基于上面我现在在〜/ .bashrc文件中有以下内容:
# uncomment for a colored prompt, if the terminal has the capability; turned # off by default to not distract the user: the focus in a terminal window # should be on the output of commands, not on the prompt #force_color_prompt=yes
…
# Add git branch if its present to PS1 parse_git_branch() { git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/' } if [ "$color_prompt" = yes ]; then PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[01;31m\]$(parse_git_branch)\[\033[00m\]\$ ' else PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w$(parse_git_branch)\$ ' fi unset color_prompt force_color_prompt
结果我现在得到:
所以它有效 。 但为什么我的user @ host的颜色已被删除? 我还希望分支名称应该是彩色的。 在它看起来像这样:
更新:我现在尝试了这个指南:
https://coderwall.com/p/fasnya/add-git-branch-name-to-bash-prompt
将此添加到.bashrc:
parse_git_branch() { git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/' } export PS1="\u@\h \[\033[32m\]\w\[\033[33m\]\$(parse_git_branch)\[\033[00m\] $ "
这工作:
请注意.bashrc我也有这个(默认):
# uncomment for a colored prompt, if the terminal has the capability; turned # off by default to not distract the user: the focus in a terminal window # should be on the output of commands, not on the prompt #force_color_prompt=yes
我还没有找到为什么该片段给出正确结果而另一个版本没有给出正确结果的原因。 有什么输入吗?
这是我的.bashrc版本,其中启用了旧代码段不起作用:
http://pastebin.com/M8kjEiH3
这个片段:
# Add git branch if its present to PS1 parse_git_branch() { git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/' } if [ "$color_prompt" = yes ]; then PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[01;31m\]$(parse_git_branch)\[\033[00m\]\$ ' else PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w$(parse_git_branch)\$ ' fi
是要替换默认的提示定义:
if [ "$color_prompt" = yes ]; then PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ ' else PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ ' fi
结束于:
unset color_prompt force_color_prompt
您发布的.bashrc
显示您在默认提示定义后添加它并unset color_prompt force_color_prompt
(第64行)。
使用片段替换默认提示定义或保留~/.bashrc
,并在第64行注释默认提示定义以及未unset color_prompt force_color_prompt
:
所以.bashrc的一部分看起来像
parse_git_branch() { git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/' } if [ "$color_prompt" = yes ]; then PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[01;31m\] $(parse_git_branch)\[\033[00m\]\$ ' else PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w$(parse_git_branch)\$ ' fi # THE SIX LINES BELOW are the default prompt and the unset (which were in the original .bashrc) #if [ "$color_prompt" = yes ]; then # PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ ' #else # PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ ' #fi #unset color_prompt force_color_prompt
Ubuntu: Show your branch name on your terminal
在〜/ .bashrc文件中添加这些行
# Show git branch name force_color_prompt=yes color_prompt=yes parse_git_branch() { git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/' } if [ "$color_prompt" = yes ]; then PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[01;31m\]$(parse_git_branch)\[\033[00m\]\$ ' else PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w$(parse_git_branch)\$ ' fi unset color_prompt force_color_prompt
现在,我按照这个https://gist.github.com/eliotsykes/47516b877f5a4f7cd52f工作,喜欢它到目前为止,虽然我打算进一步定制它。
在终端
mkdir ~/.bash
将原始
git-prompt.sh
文件从git contrib复制到~/.bash
目录: https : //github.com/git/git/blob/master/contrib/completion/git-prompt.sh在
~/.bashrc
或~/.bash_profile
(选择你通常放置任何bash自定义/设置的文件),添加以下行:source ~/.bash/git-prompt.sh # Show git branch name at command prompt export GIT_PS1_SHOWCOLORHINTS=true # Option for git-prompt.sh to show branch name in color # Terminal Prompt: # Include git branch, use PROMPT_COMMAND (not PS1) to get color output (see git-prompt.sh for more) export PROMPT_COMMAND='__git_ps1 "\w" "\n\\\$ "' # Git branch (relies on git-prompt.sh)
只要你在git repo中,你的Bash提示现在应该以颜色显示当前的git分支,表明它是否有未提交的更改。
转到主文件夹
单击Ctrl + h以显示隐藏文件。
打开。 bashrc
文件,最后粘贴下一个:
parse_git_branch() { git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/' } export PS1="\u@\h \[\033[32m\]\w\[\033[33m\]\$(parse_git_branch)\[\033[00m\] $ "
如果您打开终端,请关闭并再次打开。 请享用!!
我的问题是我没有启用该选项
将命令作为登录shell运行
终端 → 编辑 → 配置文件首选项 → 命令
更换
parse_git_branch
同
parse_git_branch 2>/dev/null
在你的PS1定义中,从此过上幸福的生活。