Source

dotfiles / _bashrc

# ~/.bashrc: executed by bash(1) for non-login shells.
# see /usr/share/doc/bash/examples/startup-files (in the package bash-doc)
# for examples

# If not running interactively, don't do anything
[ -z "$PS1" ] && return

# don't put duplicate lines in the history. See bash(1) for more options
# ... or force ignoredups and ignorespace
HISTCONTROL=ignoredups:ignorespace

# append to the history file, don't overwrite it
shopt -s histappend

# for setting history length see HISTSIZE and HISTFILESIZE in bash(1)
HISTSIZE=1000
HISTFILESIZE=2000

# check the window size after each command and, if necessary,
# update the values of LINES and COLUMNS.
shopt -s checkwinsize

# make less more friendly for non-text input files, see lesspipe(1)
[ -x /usr/bin/lesspipe ] && eval "$(SHELL=/bin/sh lesspipe)"

# set variable identifying the chroot you work in (used in the prompt below)
if [ -z "$debian_chroot" ] && [ -r /etc/debian_chroot ]; then
    debian_chroot=$(cat /etc/debian_chroot)
fi


# Prints colorized symbol for hg/git repos indicating the repo’s status
function repo_stat {
    # Git (needs to come first to detect git sub-repos in hg repos)
    if git branch &>/dev/null; then
        gitstat=$(git status 2>/dev/null | grep '\(# Untracked\|# Changes\|# Changed but not updated:\)')

        if [[ "$gitstat" =~ 'Changes to be committed' ]]; then
            color="\033[01;31m"  # Uncommited changes

        elif [[ "$gitstat" =~ 'Changes not staged for commit' || "$gitstat" =~ 'Untracked files' ]]; then
            color="\033[01;33m"  # No changes, but untracked files

        else
            color="\033[01;32m"  # No changes or untracked files
        fi
        echo -ne "${color}"

    # Mercurial
    elif hg root &>/dev/null; then
        hgstat=$(hg status)
        if [ "$hgstat" == "" ]; then
            color="\033[01;32m"  # No changes or untracked files
        else
            hgstat=$(hg status -mard)
            if [ "$hgstat" == "" ]; then
                color="\033[01;33m"  # No changes, but untracked files
            else
                color="\033[01;31m"  # Uncommited changes
            fi
        fi
        echo -ne "${color}"
    fi
}

function repo_type {
    # Git (needs to come first to detect git sub-repos in hg repos)
    if git branch &>/dev/null; then
        echo -n "±"

    # Mercurial
    elif hg root &>/dev/null; then
        echo -n "☿"

    # else
    #     echo '○'

    fi
}

# Prompt
PS1="\[\033[01;32m\]\u@\h"              # user@host (green)
PS1+="\[\033[00m\]:"                    # :
PS1+="\[\033[01;34m\]\w"                # cwd (blue)
PS1+="\[\$(repo_stat)\]\$(repo_type)"   # ☿ for hg or ± for git repos
PS1+="\[\033[00m\]\$ "                  # $


# OS X color terminal
export CLICOLOR=1
export LSCOLORS=ExFxCxDxBxegedabagacad

# enable color support of ls and also add handy aliases
if [ -x /usr/bin/dircolors ]; then
    test -r ~/.dircolors && eval "$(dircolors -b ~/.dircolors)" || eval "$(dircolors -b)"
    alias ls='ls --color=auto'
    #alias dir='dir --color=auto'
    #alias vdir='vdir --color=auto'

    alias grep='grep --color=auto'
    alias fgrep='fgrep --color=auto'
    alias egrep='egrep --color=auto'
fi

# Alias definitions.
if [ -f ~/.bash_aliases ]; then
    . ~/.bash_aliases
fi

# Some Mac OS X specific stuff
if [ `uname -s` = "Darwin" ]; then
    export PATH="/usr/local/pypy-2.0/bin:${PATH}"
    export PATH="/Library/Frameworks/Python.framework/Versions/2.7/bin:${PATH}"
    export PATH="/Library/Frameworks/Python.framework/Versions/3.2/bin:${PATH}"
    export PATH="/Library/Frameworks/Python.framework/Versions/3.3/bin:${PATH}"
    export MACOSX_DEPLOYMENT_TARGET=10.8
    export ARCHFLAGS="-arch x86_64"

    alias vim="/Applications/MacVim.app/Contents/MacOS/Vim"
fi

# set PATH so it includes user's private bin if it exists
if [ -d "$HOME/bin" ] ; then
    PATH="$HOME/bin:$PATH"
fi

# enable programmable completion features (you don't need to enable
# this, if it's already enabled in /etc/bash.bashrc and /etc/profile
# sources /etc/bash.bashrc).
if [ -f /etc/bash_completion ] && ! shopt -oq posix; then
    . /etc/bash_completion
fi

# pip bash completion
_pip_completion()
{
    COMPREPLY=( $( COMP_WORDS="${COMP_WORDS[*]}" \
                   COMP_CWORD=$COMP_CWORD \
                   PIP_AUTO_COMPLETE=1 $1 ) )
}
complete -o default -F _pip_completion pip

# Python start-up and virtualenv wrapper
export PYTHONSTARTUP=$HOME/.pythonrc
export WORKON_HOME=$HOME/.virtualenvs
venwrap=`type -P virtualenvwrapper.sh`
if [ "$venwrap" != "" ]; then
    . $venwrap
fi

# Load autojump if installed
if [ -f /usr/share/autojump/autojump.bash ]; then  # Ubuntu
    . /usr/share/autojump/autojump.bash
elif [ -f ~/.autojump/etc/profile.d/autojump.bash ]; then  # Manual (local)
    . ~/.autojump/etc/profile.d/autojump.bash
fi

# Source stuff from secret private directory ...
if [ -d ~/.private ]; then
    . ~/.private/bash_aliases.sh
fi