thayer /.bashrc

# ----------------------------------------------------------------------
# file:     ~/.bashrc
# author:   Thayer Williams - http://cinderwick.ca
# modified: July 28, 2008
# vim:
# ----------------------------------------------------------------------


# skip the rest if running non-interactively ---------------------------

[ -z "$PS1" ] && return

# bash options ---------------------------------------------------------

shopt -s cdable_vars      # if cd arg is not valid, assumes its a var defining a dir
shopt -s cdspell          # autocorrects cd misspellings
shopt -s checkwinsize     # update the value of LINES and COLUMNS after each command if altered
shopt -s cmdhist          # save multi-line commands in history as single line
shopt -s dotglob          # include dotfiles in pathname expansion
shopt -s expand_aliases   # expand aliases
shopt -s extglob          # enable extended pattern-matching features
shopt -s histappend       # append to (not overwrite) the history file
shopt -s hostcomplete     # attempt hostname expansion when @ is at the beginning ofa word
shopt -s nocaseglob       # pathname expansion will be treated as case-insensitive


# change sorting methods [a-Z] instead of [A-Z] ------------------------

export LC_COLLATE="en_CA.utf8"


# bash prompt styles ---------------------------------------------------

PS1='[\[\e[37m\]\u\[\e[31m\]@\[\e[37m\]\h\[\e[0m\]:\[\e[33m\]\w\[\e[0m\]] \$ '
#PS1='[\u@\h \W]\$ '      # default prompt
#PS1='$ '                # simple prompt
#PS1='\[\033[0;36m\]\033(0l\033(B\[\033[0m\][\[\033[1;31m\]\u\[\033[0m\]]\[\033[0;36m\]\033(0q\033(B\[\033[0m\][\[\033[1;33m\]@\h\[\033[0m\]]\[\033[0;36m\]\033(0q\033(B\[\033[0m\][\[\033[0;37m\]21:24\[\033[0m\]]\[\033[0;36m\]\033(0q\033(B\033(0q\033(B\033(0q\033(B\033(0q\033(B\033(0q\033(B\033(0q\033(B\033(0q\033(B\033(0q\033(B\[\033[0m\][\[\033[1;33m\]\w\[\033[0m\]]\n\[\033[0;36m\]\033(0m\033(B\[\033[0m\]> '


# disable ^S/^Q flow control -------------------------------------------

stty -ixon


# if exists, add ~/bin to PATH -----------------------------------------

if [ -d ~/bin ] ; then
   PATH=~/bin:$PATH
fi


# bash history ---------------------------------------------------------

export HISTFILE="$HOME/.bash_history_`hostname`"   # hostname appended to bash history filename
export HISTSIZE=10000                              # bash history will save N commands
export HISTFILESIZE=${HISTSIZE}                    # bash will remember N commands
export HISTCONTROL=ignoreboth                      # ingore duplicates and spaces (ignoreboth, ignoredups, ignorespace)

# don't append the following to history: consecutive duplicate
# commands, ls, bg and fg, and exit
HISTIGNORE='\&:fg:bg:ls:pwd:cd ..:cd ~-:cd -:cd:jobs:set -x:ls -l:ls -l'
HISTIGNORE=${HISTIGNORE}':%1:%2:shutdown*'
export HISTIGNORE


# env vars -------------------------------------------------------------

export OOO_FORCE_DESKTOP=gnome      # openoffice preferences
export BROWSER="links '%s' &"

# define 1st and 2nd choice editors and pagers
export EDITOR=/usr/bin/vi
export PAGER=/bin/more
my_editor=vim
my_pager=less
type $my_editor >/dev/null 2>&1 && export EDITOR=$my_editor
type $my_pager >/dev/null 2>&1 && export PAGER=$my_pager


# bash sudo completion -------------------------------------------------
complete -cf sudo
if [ -f /etc/bash_completion ]; then
  . /etc/bash_completion
fi


# when connecting to SSH, start or reattach screen session -------------

if [ -n "$SSH_CONNECTION" ] && [ -z "$SCREEN_EXIST" ]; then
  export SCREEN_EXIST=1
  screen -DR
fi


# aliases --------------------------------------------------------------

alias ..='cd ..'
alias ...='cd ../..'
alias ....='cd ../../..'
alias .....='cd ../../../..'
alias hist='history | grep $1'            # search cmd history
alias cp='cp -i'
alias df='df -h'                          # human-readable sizes
alias ducks='du -cks * |sort -rn |head -11' # disk hog
alias free='free -m'                      # show sizes in MB
alias ls='ls --color=auto -F'             # colourized list
alias ll='ls -lh'                         # list detailed with human-readable sizes
alias la='ls -a'                          # list all files
alias dir='ls -1'                         # windows-style list
alias f='find | grep'                     # quick search
alias c='clear'                           # clear screen
alias xp='xprop | grep "WM_WINDOW_ROLE\|WM_CLASS" && echo "WM_CLASS(STRING) = \"NAME\", \"CLASS\""' # get xprop CLASS and NAME
alias sv='sudo vim'
alias sps='ps aux | grep -v grep | grep'  # search process
alias grep='grep --color=auto'            # colourized grep
alias egrep='egrep --color=auto'          # colourized egrep
alias fixres="xrandr --size 1680x1050"    # reset resolution
alias clam='clamscan --bell -i'           # clamav scan a file
alias clamt='clamscan -r --bell -i ~/tmp' # clamav scan ~/tmp
alias getip='wget http://checkip.dyndns.org/ -O - -o /dev/null | cut -d: -f 2 | cut -d\< -f 1'
alias obr='vim ~/.config/openbox/rc.xml'
alias obm='vim ~/.config/openbox/menu.xml'
alias oba='vim ~/.config/openbox/autostart.sh'
alias psm="echo '%CPU %MEM   PID COMMAND' && ps hgaxo %cpu,%mem,pid,comm | sort -nrk1 | head -n 10 | sed -e 's/-bin//' | sed -e 's/-media-play//'"
#unalias -a               # uncomment to unalias everything 


# ssh/encrypt aliases --------------------------------------------------

alias k='ssh-add'
alias kd='ssh-add -D'


# pkgbuild aliases -----------------------------------------------------

alias vp='vim PKGBUILD'
alias clean='rm -rf pkg/ src/ *~'         # clean up the build dir
alias svnid='svn propset svn:keywords Id PKGBUILD'


# pacman aliases -------------------------------------------------------

alias pacup='sudo pacman -Syu'            # sync and update
alias pacin='sudo pacman -S'              # install pkg
alias pacout='sudo pacman -Rns'           # remove pkg and the deps it installed
alias pacs="pacman -Sl | cut -d' ' -f2 | grep " #
alias pac="pacsearch"                     # colorize pacman (pacs)
pacsearch () 
{
  echo -e "$(pacman -Ss $@ | sed \
  -e 's#core/.*#\\033[1;31m&\\033[0;37m#g' \
  -e 's#extra/.*#\\033[0;32m&\\033[0;37m#g' \
  -e 's#community/.*#\\033[1;35m&\\033[0;37m#g' \
  -e 's#^.*/.* [0-9].*#\\033[0;36m&\\033[0;37m#g' )"
}


# functions ------------------------------------------------------------

# s - gnu screen function
# usage: lists screen sessions, otherwise
#        s  reattaches to , otherwise
#        s  creates a new session .
s ()
{
  if [[ $1 ]]; then
    screen -dRR -S $HOSTNAME.$1
  else
    screen -ls
  fi
}

# nohup - run command detached from terminal and without output
# usage: nh 
nh()
{
  nohup "$@" &>/dev/null &
}

# ex - archive extractor
# usage: ex 
ex ()
{
  if [ -f $1 ] ; then
    case $1 in
      *.tar.bz2)   tar xjf $1   ;;
      *.tar.gz)    tar xzf $1   ;;
      *.bz2)       bunzip2 $1   ;;
      *.rar)       rar x $1     ;;
      *.gz)        gunzip $1    ;;
      *.tar)       tar xf $1    ;;
      *.tbz2)      tar xjf $1   ;;
      *.tgz)       tar xzf $1   ;;
      *.zip)       unzip $1     ;;
      *.Z)         uncompress $1;;
      *.7z)        7z x $1      ;;
      *)           echo "'$1' cannot be extracted via ex()" ;;
    esac
  else
    echo "'$1' is not a valid file"
  fi
}

# roll - archive wrapper
# usage: roll  ./foo ./bar
roll ()
{
  FILE=$1
  case $FILE in
    *.tar.bz2) shift && tar cjf $FILE $* ;;
    *.tar.gz) shift && tar czf $FILE $* ;;
    *.tgz) shift && tar czf $FILE $* ;;
    *.zip) shift && zip $FILE $* ;;
    *.rar) shift && rar $FILE $* ;;
  esac
}

# mktar - tarball wrapper
# usage: mktar 
function mktar() { tar czf "${1%%/}.tar.gz" "${1%%/}/"; }

# calc - simple calculator
# usage: calc 
function calc() { echo "$*" | bc; }

# define - fetch word defnition from google
# usage: define 
define ()
{
  lynx -dump "http://www.google.com/search?hl=en&q=define%3A+${1}&btnG=Google+Search" | grep -m 5 -w "*"  | sed 's/;/ -/g' | cut -d- -f5 > /tmp/templookup.txt
  if [[ -s  /tmp/templookup.txt ]] ;then    
    until ! read response
      do
      echo "${response}"
      done < /tmp/templookup.txt
    else
      echo "Sorry $USER, I can't find the term \"${1} \""                
  fi    
  rm -f /tmp/templookup.txt
}

# absbuild - quickly build and upgrade a pkg from ABS
# usage: absbuild 
absbuild ()
{
  ABSPATH=`find /var/abs -type d -name $1`
  mkdir -p ~/.abs/$1
  cp -R $ABSPATH/* ~/.abs/$1
  cd ~/.abs/$1
  $EDITOR PKGBUILD
}

# search the vim reference manual for a keyword
# usage: :h 
:h() {  vim --cmd ":silent help $@" --cmd "only"; }

# mkmine - recursively change ownership to $USER:$USER
# usage:  mkmine, or
#         mkmine 
function mkmine() { sudo chown -R ${USER}:${USER} ${1:-.}; }

# sanitize - set file/directory owner and permissions to normal values (644/755)
# usage: sanitize 
sanitize()
{
  chmod -R u=rwX,go=rX "$@"
  chown -R ${USER}:users "$@"
}

# remindme - a simple reminder
# usage: remindme