Reputation: 79725
On Git Bash on Windows I get nice coloring and the current branch is shown, like this:
How can I get the same coloring and prompt on Linux? On Linux I use the regular terminal, which doesn't show the current branch.
Upvotes: 10
Views: 9129
Reputation: 61
If you would like to retain the same coloring of your existing linux terminal and also show the current git branch, you can add the following to your default PS1.
PS1 basically tells your bash prompt what to display. Ref: How to Change / Set up bash custom prompt (PS1) in Linux
I am using Ubuntu 20.04 and the default PS1 is found under if [ "$color_prompt" = yes ]; then
in the ~/.bashrc file.
Steps:
if [ "$color_prompt" = yes ]; then
#show git branch
show_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[00m\] \[\033[31m\]\$(show_git_branch)\[\033[00m\]$ "
else
PS1="${debian_chroot:+($debian_chroot)}\u@\h:\w \$(show_git_branch)\$ "
fi
The entire thing should look something like this:
#show git branch
show_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[00m\] \[\033[31m\]\$(show_git_branch)\[\033[00m\]$ "
else
PS1="${debian_chroot:+($debian_chroot)}\u@\h:\w \$(show_git_branch)\$ "
fi
It should looks something like this:
Upvotes: 6
Reputation: 984
If you are using bash,
I use the following on my ~/.bashrc:
show_git_branch() {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/'
}
export PS1="\[\033[0;37m\]\u@\h\[\033[0;37m\] \w \[\033[31m\]\$(show_git_branch)\[\033[00m\]$\[\033[00m\] "
A sample of what it looks like:
You just need to add the code to your .bashrc file and run source ~/.bashrc
.
Upvotes: 3