Bash Profile

I find myself working in iTerm a lot. Well to be honest i prefair a terminal over a Finder window any day.
So i find myself running the same commands over and over, {{ git status }} over and over to check if any files have changed.
Checking which branch i’m currently on.

I spent a lot of time altering my bash profile to enable colours by default, changing the prompt layout, setting up aliases etc.
I finally found a brilliant template online by Jacob Tomlinson at Terminal Piperita Theme

The prompt integrates into git, so if git is enabled in the current directory it will add the current branch to the prompt and tell me if there have been any changes made. It’ll give me the user my terminal is logged in as and the hostname of the machine along with the current folder all with some pretty colours that change if i am logged in as root too.

Anyway here is my current bash profile

export CLICOLOR=1
export LSCOLORS=GxFxCxDxBxegedabagaced
 
### Start Piperita Theme ###
# Theme by Jacob Tomlinson
# https://github.com/killfall/terminal-piperita
 
# Aliases to make ls easier to use in different modes, taken from Red Hat EL 6
alias ls='ls -GFh'
alias ll="ls -ll"
alias la="ll -a"
 
# Set ls colors
export LSCOLORS=ExFxBxDxCxegedabagacad 
 
# Alias to set color in grep
alias grep="grep --color=auto"
 
# Environment variables to set prompt format and color 
export COLOR_BOLD="\[\e[1m\]"
export COLOR_DEFAULT="\[\e[0m\]"
 
# Test if user is root and set user color appropriately
if [[ $(id -u) == 0 ]]
then
    export PS1USERCOLOR="\[\033[31m\]"
else
    export PS1USERCOLOR="\[\033[36m\]"
fi
 
# get current branch in git repo
function parse_git_branch() {
        BRANCH=$(git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/')
        if [ ! "${BRANCH}" == "" ]
        then
                STAT=$(parse_git_dirty)
                echo " [${BRANCH}${STAT}]"
        else
                echo ""
        fi
}
 
# get current status of git repo
function parse_git_dirty {
        status=$(git status 2>&1 | tee)
        dirty=$(echo -n "${status}" 2> /dev/null | grep "modified:" &> /dev/null; echo "$?")
        untracked=$(echo -n "${status}" 2> /dev/null | grep "Untracked files" &> /dev/null; echo "$?")
        ahead=$(echo -n "${status}" 2> /dev/null | grep "Your branch is ahead of" &> /dev/null; echo "$?")
        newfile=$(echo -n "${status}" 2> /dev/null | grep "new file:" &> /dev/null; echo "$?")
        renamed=$(echo -n "${status}" 2> /dev/null | grep "renamed:" &> /dev/null; echo "$?")
        deleted=$(echo -n "${status}" 2> /dev/null | grep "deleted:" &> /dev/null; echo "$?")
        bits=''
        if [ "${renamed}" == "0" ]; then
                bits=">${bits}"
        fi
        if [ "${ahead}" == "0" ]; then
                bits="*${bits}"
        fi
        if [ "${newfile}" == "0" ]; then
                bits="+${bits}"
        fi
        if [ "${untracked}" == "0" ]; then
                bits="?${bits}"
        fi
        if [ "${deleted}" == "0" ]; then
                bits="x${bits}"
        fi
        if [ "${dirty}" == "0" ]; then
                bits="!${bits}"
        fi
        if [ ! "${bits}" == "" ]; then
                echo " ${bits}"
        else
                echo ""
        fi
}
 
export PS1="$PS1USERCOLOR\u\[\033[m\]@\[\033[32m\]\h\[\033[m\]:\[\033[33;1m\]\W\`if type parse_git_branch > /dev/null 2>&1; then parse_git_branch; fi\`\`if [ \$? = 0 ]; then echo -e '\[\033[m\]\$'; else echo -e '\[\e[31m\]\$'; fi\` \[\033[m\]"
 
export CLICOLOR=1
 
### End Piperita Theme ###

Leave a Reply

Your email address will not be published. Required fields are marked *