Skip to content

Instantly share code, notes, and snippets.

@tobiassjosten
Created February 15, 2011 22:47
Show Gist options
  • Star 49 You must be signed in to star a gist
  • Fork 36 You must be signed in to fork a gist
  • Save tobiassjosten/828432 to your computer and use it in GitHub Desktop.
Save tobiassjosten/828432 to your computer and use it in GitHub Desktop.
# Configure colors, if available.
if [ -x /usr/bin/tput ] && tput setaf 1 >&/dev/null; then
c_reset='\[\e[0m\]'
c_user='\[\e[0;32m\]'
c_path='\[\e[1;34m\]'
c_git_clean='\[\e[0;37m\]'
c_git_staged='\[\e[0;32m\]'
c_git_unstaged='\[\e[0;31m\]'
else
c_reset=
c_user=
c_path=
c_git_clean=
c_git_staged=
c_git_unstaged=
fi
# Add the titlebar information when it is supported.
case $TERM in
xterm*|rxvt*)
TITLEBAR='\[\e]0;\u@\h: \w\a\]';
;;
*)
TITLEBAR="";
;;
esac
# Function to assemble the Git parsingart of our prompt.
git_prompt ()
{
GIT_DIR=`git rev-parse --git-dir 2>/dev/null`
if [ -z "$GIT_DIR" ]; then
return 0
fi
GIT_HEAD=`cat $GIT_DIR/HEAD`
GIT_BRANCH=${GIT_HEAD##*/}
if [ ${#GIT_BRANCH} -eq 40 ]; then
GIT_BRANCH="(no branch)"
fi
STATUS=`git status --porcelain`
if [ -z "$STATUS" ]; then
git_color="${c_git_clean}"
else
echo -e "$STATUS" | grep -q '^ [A-Z\?]'
if [ $? -eq 0 ]; then
git_color="${c_git_unstaged}"
else
git_color="${c_git_staged}"
fi
fi
echo "[$git_color$GIT_BRANCH$c_reset]"
}
# Thy holy prompt.
PROMPT_COMMAND="$PROMPT_COMMAND PS1=\"${TITLEBAR}${c_user}\u${c_reset}@${c_user}\h${c_reset}:${c_path}\w${c_reset}\$(git_prompt)\$ \" ;"
@made-by-love
Copy link

PROMPT_COMMAND="$PROMPT_COMMAND should be PROMPT_COMMAND="

@ddeath
Copy link

ddeath commented Mar 6, 2015

This is not true:
if [ ${#GIT_BRANCH} -eq 40 ]; then
GIT_BRANCH="(no branch)"

when name of my branch is:
PW-50-aaaaaaaaa-bbbbb-cc-ddddddddd-eee-f

@villu164
Copy link

If you have trouble with the snippet, try doing a
echo $PROMPT_COMMAND
Because my previous PROMPT_COMMAND didn't include ending semicolon and gave this
update_terminal_cwd PS1="\[\e]0;\u@\h: \w\a\]\[\e[0;32m\]\u\[\e[0m\]@\[\e[0;32m\]\h\[\e[0m\]:\[\e[1;34m\]\w\[\e[0m\]$(git_prompt)$ " ;
Simply adding the semicolon here does the trick
PROMPT_COMMAND="$PROMPT_COMMAND; PS1

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