A nice .bashrc script

26 Aug 2015


unalias -a

# 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

############################# PS1 support functions ##################

# print non-zero exit
_pnze() {
    local e=$1
    if [ "$e" -ne "0" ]; then
        echo "[$e]"
    fi
}

# print git branch
_pgb() {
    txt=$(git branch 2>&1)
    local e=$?
    if [ "$e" -eq "0" ]; then
        #local b=$(echo "$txt" | grep '*' | awk '{ print $1 }')
        # echo "$b"
        # local b=$(echo "$txt" | grep '*' | cut --delimiter=' ' --fields=2)
        # echo "$b"
        local b=$(echo "$txt" | grep '*')
        echo "${b:2}"
    fi
}

# print git status
_pgs() {
    txt=$(git status --porcelain 2>&1)
    local e=$?
    if [ "$e" -eq "0" ] && [ -n "$txt" ]; then
        echo "+"
    fi
}

# print git branch and status
_pgbs() {
    local b=$(_pgb)
    local s=$(_pgs)
    if [ -n "$b" ]; then
        echo "($b$s)"
    fi
}

############################# PS1 ##################

# IMPORTANT: must capture exit status $? of previous command
# FIRST, otherwise, later commands in PS1 will overwrite $?
PS1='$(_pnze $?)\u@\h:\w$(_pgbs)\$ '
# This changes the terminal window title
PROMPT_COMMAND='echo -ne "\033]0;$(_pnze $?)${PWD/$HOME/\~}$(_pgbs)\007"'

################## bash completion #################

# 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 ]; then
    . /etc/bash_completion
fi

############## "aliases" ####################

h() {
    if [ -n "$1" ]; then
        history | grep "$1"
    else
        history
    fi
}

# Runs vi as vim

vi() {
    vim "$@"
}


# Sets the terminal title

set-prompt() {
    PROMPT_COMMAND="echo -ne \"\033]0;${1:-TITLE NOT SET}\007\""
}

######## ls "aliases" ####################

# see http://blog.twistedcode.org/2008/04/lscolors-explained.html
export LS_COLORS='di=34:fi=0:ln=35:pi=31:so=31:bd=31:cd=31:or=41:mi=41:ex=32'

# don't forget 'command' and 'builtin'
# to get a list of builtins, run 'enable'

ls() {
    command ls --color --file-type "$@"
}

C() {
    command ls --classify "$@"
}

c() {
    command ls --color "$@"
}

L() {
    command ls --color --file-type -ltrh "$@"
}

############## HOMES #####################

export GROOVY_HOME=/usr/local/groovy-2.2.1
export JAVA_HOME=/usr/local/jdk1.7.0_45
export GRADLE_HOME=/usr/local/gradle-1.10
export ANT_HOME=/usr/local/apache-ant-1.9.3

############## PATH #####################

export PATH=/usr/local/pgsql-9.3/bin:$PATH
export PATH=$JAVA_HOME/bin:$PATH
export PATH=$GRADLE_HOME/bin:$PATH
export PATH=$GROOVY_HOME/bin:$PATH
export PATH=$ANT_HOME/bin:$PATH