[Insight-developers] Bash shell prompt customization

Arnaud GELAS arnaud_gelas at hms.harvard.edu
Mon Sep 27 15:43:01 EDT 2010


  On 09/27/2010 03:38 PM, Matthew McCormick (thewtex) wrote:
>
>
>     I'm sure the experts already know this, but if you are using the bash
>     shell, you can customize the prompt to show which git branch is
>     active.
>
>     Here is what I added to my ~/.bashrc file:
>
>     parse_git_branch()
>      {
>      git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/'
>      }
>     export
>     PS1="[\[\e[01;34m\]\W\[\e[31m\]\$(parse_git_branch)\[\e[00m\]]\[\e[00m\]
>     "
>
>
> Hi Bill,
>
> This is a great tip.
>
> Note that git already comes with some machinery to do this.  Here is 
> what the relevant lines looks like in my .bashrc
> source /usr/share/bash-completion/git
> export 
> PS1='\[\033[01;32m\]\u\[\033[00;34m\]@\[\033[01;35m\]\h\[\033[01;36m\] 
> \w$(__git_ps1 " (%s)")\[\033[00;34m\]\$\[\033[00m\] '
>
> There are tips in the comments of /usr/share/bash-completion/git for 
> using __git_ps1.  There are a couple of customization available.
>
> Matt

With this one (provided by JC), you can also know when you have local 
changes

# git status with a dirty flag
function __git_status_flag {
   git_status="$(git status 2> /dev/null)"
   remote_pattern="^# Your branch is (.*) of"
   diverge_pattern="# Your branch and (.*) have diverged"
   if [[ ! ${git_status}} =~ "working directory clean" ]]; then
state="?"
     spacer=" "
   fi

if [[ ${git_status} =~ ${remote_pattern} ]]; then
spacer=" "
     if [[ ${BASH_REMATCH[1]} == "ahead" ]]; then
remote="?"
     else
remote="?"
     fi
fi

if [[ ${git_status} =~ ${diverge_pattern} ]]; then
remote="?"
     spacer=" "
   fi

echo "${state}${remote}${spacer}"
}

# the prompt itself
PS1='\[\e[32m\]\u@\[\e[1m\]\h\[\e[22m\]:\[\e[1;34m\]\W\[\e[22;35m\]$(__git_ps1 
" [\[\e[33m\]$(__git_status_flag)\[\e[35m\]%s]")\[\e[33m\] \$ \[\e[0m\]'
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.itk.org/mailman/private/insight-developers/attachments/20100927/5c5e2b74/attachment.htm>


More information about the Insight-developers mailing list