Skip to content

Instantly share code, notes, and snippets.

@donovanh
Forked from oisin/gist:4444722
Last active December 18, 2015 16:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save donovanh/5814722 to your computer and use it in GitHub Desktop.
Save donovanh/5814722 to your computer and use it in GitHub Desktop.
A handy bash prompt customization that shows the working directory followed by the current branch, and an asterisk if the branch is dirrrrty.
# Highlighting on prompt
RED="\[\033[0;31m\]"
YELLOW="\[\033[0;33m\]"
GREEN="\[\033[0;32m\]"
COLOR_NONE="\[\e[0m\]"
parse_git_branch() {
git_status="$(git status 2> /dev/null)"
branch_pattern="^# On branch ([^${IFS}]*)"
remote_pattern="# Your branch is (.*) of"
diverge_pattern="# Your branch and (.*) have diverged"
if [[ ! ${git_status}} =~ "working directory clean" ]]; then
state="${RED} "
fi
# add an else if or two here if you want to get more specific
if [[ ${git_status} =~ ${remote_pattern} ]]; then
if [[ ${BASH_REMATCH[1]} == "ahead" ]]; then
remote="${YELLOW}↑"
else
remote="${YELLOW}↓"
fi
fi
if [[ ${git_status} =~ ${diverge_pattern} ]]; then
remote="${YELLOW}↕"
fi
if [[ ${git_status} =~ ${branch_pattern} ]]; then
branch=${BASH_REMATCH[1]}
echo " (${branch})${remote}${state}"
fi
}
prompt_func() {
git rev-parse --git-dir &> /dev/null
previous_return_value=$?
prompt="\w${GREEN}$(parse_git_branch)${COLOR_NONE} "
if [[ $previous_return_value == 0 ]]; then
PS1="${prompt}\$ "
else
PS1=$PS1_DEFAULT
fi
}
PS1_DEFAULT=$PS1
PROMPT_COMMAND=prompt_func
set_tab_title() {
echo -n -e "\033]0;${PWD##*/}\007"
}
PROMPT_COMMAND="set_tab_title ; $PROMPT_COMMAND"
@oisin
Copy link

oisin commented Nov 21, 2013

Ok. Forget this - get on board with zsh and oh-my-zsh instead :D

https://github.com/robbyrussell/oh-my-zsh

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment