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:
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
Ubuntu: Show your branch name on your terminal
Add these lines in your ~/.bashrc file
Reload the .bashrc file with this command:
For now, I followed this https://gist.github.com/eliotsykes/47516b877f5a4f7cd52f and working, liking it so far, though I'm planning to customize it further.
Quick hack:
~/.bashrc
:~/.bashrc
:More detail: https://medium.com/@thucnc/how-to-show-current-git-branch-with-colors-in-bash-prompt-380d05a24745
Append the lines below to
~/.bashrc
:Go to home folder
click Ctrl+h to show hidden files.
Open .
bashrc
file and at the end paste the next:In case you have your terminal opened, close and open again. Enjoy!!
I use bash-git-prompt. It's configurable and easier than writing your own, which I imagine is what many readers are looking for.
Why bother with using
sed
? as in ...Much easier to just use:
It outputs the current branch and no extra characters!
Topic is old but I will post being able to help someone. In the .bashrc file located inside the user's local folder, replace the existing PS1 with this one:
If you want to change the color of the branch change the color before the parameter $ (__ git_ps1 "(% s)") that is, change this value:
color chart https://gist.github.com/avelino/3188137
My variant for KUbuntu 20.04 LTS, derived from the original value of the
PS1
: