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:
Best Answer
This snippet:
Is meant to replace the default prompt definition:
Which ends with:
The
.bashrc
you posted shows you're adding it after the default prompt definition andunset color_prompt force_color_prompt
(line #64).Either replace the default prompt definition with the snippet or leave your
~/.bashrc
as it is and comment the default prompt definition along withunset color_prompt force_color_prompt
on line #64:So part of your .bashrc could look like