Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PS1 line with Git current branch and colors

Here is my current PS1:

export PS1='[\u@\h \W$(__git_ps1 " (%s)")]\$ ' 

How can I display the current branch in a different color?

like image 399
cfischer Avatar asked Nov 09 '10 13:11

cfischer


People also ask

How do I show a branch in bash?

For example, I open it using the VS Code using this command: code . bash_profile. Then just paste the following codes to your Bash. will fetch the branch name & then through PS1 you can show it in your terminal.

Where is __ git_ps1 defined?

The __git_ps1 is a shell function. You'll also notice a bit above (line #154 in my version) that /etc/git-completion. bash is being sourced in as well as /etc/git-prompt.sh . It's /etc/git-prompt.sh that defines the __git_ps1 function (Line #273 in my version) is defined.


2 Answers

Here is, part by part (and no Ruby):

function color_my_prompt {     local __user_and_host="\[\033[01;32m\]\u@\h"     local __cur_location="\[\033[01;34m\]\w"     local __git_branch_color="\[\033[31m\]"     #local __git_branch="\`ruby -e \"print (%x{git branch 2> /dev/null}.grep(/^\*/).first || '').gsub(/^\* (.+)$/, '(\1) ')\"\`"     local __git_branch='`git branch 2> /dev/null | grep -e ^* | sed -E  s/^\\\\\*\ \(.+\)$/\(\\\\\1\)\ /`'     local __prompt_tail="\[\033[35m\]$"     local __last_color="\[\033[00m\]"     export PS1="$__user_and_host $__cur_location $__git_branch_color$__git_branch$__prompt_tail$__last_color " } color_my_prompt 

Looks like this (with my own terminal palette):

Colored prompt

Also, see this and this article.

like image 172
shaman.sir Avatar answered Sep 28 '22 10:09

shaman.sir


You can wrap the part that you want in colour with the following:

\e[0;32m - sets colour (in this case, to green)

\e[m - sets colour back to the default

For example, this sets the prompt to the last token of the current path, in green, followed by $ in the default colour:

export PS1='\e[0;32m\w\e[m $' 

Other colours are available too. Have a look at this article under colorization for a comprehensive list of alternatives.

like image 34
Simon Whitaker Avatar answered Sep 28 '22 10:09

Simon Whitaker