I am using this guide to show the branch name in gnome terminal (Ubuntu 15.10) when working in a git repository. Based on the above I now have the below in my ~/.bashrc file:
# 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
As a result I now get:
so it works. But why has the coloring of my user@host been removed? And I would also expect that the branch name should be colored. Before it looked like this:
UPDATE: I have now tried this guide instead:
https://coderwall.com/p/fasnya/add-git-branch-name-to-bash-prompt
adding this to .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\] $ "
and that works:
Notice in .bashrc I also have this (default):
# 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
I have yet to find the reason why that snippet gives the correct result and the other version does not. Any input on this?
Here is the version of my .bashrc that has the old snippet enabled that does not work:
force_color_prompt
uncommented before? – muru Feb 07 '16 at 08:13.bashrc
? IIRC the default.bashrc
doesn't enable colour prompts, so you have to change it to show colours. It depends on what you changed. – muru Feb 07 '16 at 08:20\[\033[32m\]
and similar parts) without any checks. The old snippet checks if colour prompts are enabled before using them, and if they're not enabled, well. – muru Feb 07 '16 at 08:25.bashrc
. – muru Feb 07 '16 at 08:29force_color_prompt
didn't help. – muru Feb 07 '16 at 08:39.bashrc
you just posted confirms my theory, you're setting the prompt afterunset color_prompt force_color_prompt
. Remove line 64 and it'll work. – kos Feb 07 '16 at 08:42.bashrc
too much. If you mess up, you can always get the original from/etc/skel/.bashrc
. – muru Feb 07 '16 at 08:52git-prompt.sh
instead. Nevertheless, there's no need to usegit branch
and pipe tosed
. Just usegit describe --contains --all HEAD
– CervEd Jun 22 '21 at 19:00