simple bash shell prompt modification to add git branch information to the shell prompt
#### shell prompt color
export COLOR_NC='\e[0m' # No Color
export COLOR_WHITE='\e[1;37m'
export COLOR_BLACK='\e[0;30m'
export COLOR_BLUE='\e[0;34m'
export COLOR_LIGHT_BLUE='\e[1;34m'
export COLOR_GREEN='\e[0;32m'
export COLOR_LIGHT_GREEN='\e[1;32m'
export COLOR_CYAN='\e[0;36m'
export COLOR_LIGHT_CYAN='\e[1;36m'
export COLOR_RED='\e[0;31m'
export COLOR_LIGHT_RED='\e[1;31m'
export COLOR_PURPLE='\e[0;35m'
export COLOR_LIGHT_PURPLE='\e[1;35m'
export COLOR_BROWN='\e[0;33m'
export COLOR_YELLOW='\e[1;33m'
export COLOR_GRAY='\e[0;30m'
export COLOR_LIGHT_GRAY='\e[0;37m'
### this function set up the prompt
function setgitprompt {
export GITPROMPT=$(/usr/bin/git branch --no-color 2> /dev/null | grep "\*" | sed -e 's/\(.*\)/\\e[0;35m[\1]\\e[0m/' -e 's/ //g');
#### prompt looks like: user@hostname:path[*git_branch]$
PS1="${COLOR_RED}\u${COLOR_NC}@${COLOR_LIGHT_BLUE}\h${COLOR_NC}:${COLOR_WHITE}\w${COLOR_YELLOW}${GITPROMPT}\$${COLOR_NC} "
}
### hijack cd command to trigger shell prompt update, also enable back command
function cd_and_git_check {
export PREV=$(pwd);
cd $1;
setgitprompt;
}
### hijack git command to allow git branch switch update on prompt
function mygit {
/usr/bin/git $*;
setgitprompt;
}
PS1="${COLOR_RED}\u${COLOR_NC}@${COLOR_LIGHT_BLUE}\h${COLOR_NC}:${COLOR_WHITE}\w${COLOR_YELLOW}${GITPROMPT}\$${COLOR_NC} "
TERM=xterm-color;
export CLICOLOR=1
alias cd='cd_and_git_check';
alias back='cd_and_git_check ${PREV}';
alias git='mygit';