Skip to content

Instantly share code, notes, and snippets.

@op1ekun
Created August 23, 2013 21:24
Show Gist options
  • Save op1ekun/6324189 to your computer and use it in GitHub Desktop.
Save op1ekun/6324189 to your computer and use it in GitHub Desktop.
Git branch + git status in prompt
function _fancy_prompt {
local RED="\[\033[01;31m\]"
local GREEN="\[\033[01;32m\]"
local YELLOW="\[\033[01;33m\]"
local BLUE="\[\033[01;34m\]"
local WHITE="\[\033[00m\]"
local PROMPT=""
# Working directory
PROMPT=$PROMPT"$WHITE\w"
# Git-specific
local GIT_STATUS=$(git status 2> /dev/null)
if [ -n "$GIT_STATUS" ] # Are we in a git directory?
then
# Open paren
PROMPT=$PROMPT" $GREEN("
# Branch
PROMPT=$PROMPT$(git branch --no-color 2> /dev/null | sed -e "/^[^*]/d" -e "s/* \(.*\)/\1/")
# Warnings
PROMPT=$PROMPT$YELLOW
# Merging
echo $GIT_STATUS | grep "Unmerged paths" > /dev/null 2>&1
if [ "$?" -eq "0" ]
then
PROMPT=$PROMPT$RED"|MERGING"
fi
# Dirty flag
echo $GIT_STATUS | grep "nothing added to commit" > /dev/null 2>&1
if [ "$?" -eq 0 ]
then
PROMPT=$PROMPT
else
echo $GIT_STATUS | grep "nothing to commit" > /dev/null 2>&1
if [ "$?" -eq 0 ]
then
PROMPT=$PROMPT
else
PROMPT=$PROMPT" *"
fi
fi
# Warning for no email setting
git config user.email | grep @ > /dev/null 2>&1
if [ "$?" -ne 0 ]
then
PROMPT=$PROMPT" !!! NO EMAIL SET !!!"
fi
# Closing paren
PROMPT=$PROMPT"$GREEN)"
fi
# Final $ symbol
PROMPT=$PROMPT" $WHITE\$$WHITE "
export PS1=$PROMPT
}
export PROMPT_COMMAND="_fancy_prompt"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment