Source

etc / bash / bashrc

Full commit
# ~/.bashrc -> ~/etc/bash/bashrc

# if we haven't sourced our bash_profile, do so
if ! [[ $BASH_PROFILE ]]; then
    source $HOME/etc/bash/bash_profile
	return
fi

#
# functions for initialization
#
add_path () {
	path="$1"
	shift

	while [ $# -gt 0 ]; do
		new="$(echo $1 | sed 's_/$__')" # no trailing /
		expand=$(eval echo \$$path)

		oIFS="$IFS"
		IFS=":"

		for dir in $expand; do
			if [ "$dir" == "$new" ]; then
				shift
				IFS="$oIFS"
				continue 2
			fi
		done

		if [ -z "$expand" ]; then
			eval $path="$new"
		else
			eval $path="$new:\$$path"
		fi

		IFS="$oIFS"
		shift
	done

	return 0
}

init_colors () {
	export reset="$(tput sgr0)"

	export bold="$(tput bold)"
	export dim="$(tput dim)"
	export under="$(tput smul)"
	export ununder="$(tput rmul)"
	export blink="$(tput blink)"
	export rev="$(tput rev)"
	export invis="$(tput invis)"

	export black="$(tput setaf 0)"
	export red="$(tput setaf 1)"
	export green="$(tput setaf 2)"
	export yellow="$(tput setaf 3)"
	export blue="$(tput setaf 4)"
	export magenta="$(tput setaf 5)"
	export cyan="$(tput setaf 6)"
	export white="$(tput setaf 7)"
	# export default="$(tput setaf 9)" # not true now with 256 color


	export bg_black="$(tput setab 0)"
	export bg_red="$(tput setab 1)"
	export bg_green="$(tput setab 2)"
	export bg_yellow="$(tput setab 3)"
	export bg_blue="$(tput setab 4)"
	export bg_magenta="$(tput setab 5)"
	export bg_cyan="$(tput setab 6)"
	export bg_white="$(tput setab 7)"
	# export bg_default="$(tput setab 9)" # not true now with 256 color

	# colors for grep and man pages
	export GREP_OPTIONS='--color=auto'
	export GREP_COLORS='sl=0:cx=0:rv:mt=33:fn=36:ln=32:se=35'

	export LESS_TERMCAP_mb=$bold$red        # begin blinking
	export LESS_TERMCAP_md=$cyan            # begin bold
	export LESS_TERMCAP_me=$reset           # end mode
	export LESS_TERMCAP_se=$reset           # end standout-mode
	export LESS_TERMCAP_so=$bold$bg_blue    # begin standout-mode - info box
	export LESS_TERMCAP_ue=$reset           # end underline
	export LESS_TERMCAP_us=$yellow
}

init_misc () {
	complete -F _command e n t vie
	complete -o nospace -F _cd cs
	complete -o default -F _longopt a s l ll la lal

	# ignore duplicates and lines with a leading space
	# append history to .bash_history
	export HISTCONTROL=ignoreboth
	shopt -s histappend

	shopt -s cdspell        # forgive simple cd spelling mistakes
	shopt -s checkhash      # check if a hashed file exists, if not, search
	shopt -s checkwinsize   # check window size and set in LINES and COLUMNS
	shopt -s cmdhist        # try to save all lines of a multi-line command in one entry
	shopt -s dirspell       # forgive simple directory spelling mistakes
	shopt -s extglob        # extended glob options
	shopt -s globstar       # ** "match a files and zero or more directories and subdirs
	shopt -s gnu_errfmt     # gnu error message format (HUH?)
	shopt -s hostcomplete   # attempt to complete hostnames when @ is found
	shopt -s lithist        # save multi-line commands with embedded newlines, not semicolons
}

init_alias () {
	alias vi=vim
	alias lvi="vim -c \"normal '''0\""

	alias cd..='cd ..'

	eval "$(dircolors -b $HOME/etc/bash/dircolors)"
	alias ls='ls -F --color=auto'
	alias s='ls'
	alias l='ls -lh'
	alias ll='ls -lh'
	alias a='ls -lha'
	alias la='ls -a'
	alias lal='ls -lha'

	alias df='df -h'
	alias free='free -m'
	alias mount='mount | column -t'

	alias ha='history -a'
	alias hn='history -n'
	
	alias diff='colordiff'

	alias suvi='sudo -e'

	alias m='rlwrap mail'
}


init_prompt () {
	PS1="\[$reset\][\[$green\]\h \[$magenta\]\W \[$red\]\!\[$reset\]]\$ "
	[[ "$TERM" =~ "rxvt" ]] && PROMPT_COMMAND='echo -ne "\e]0;${USER}@${HOSTNAME%%.*}: ${PWD/$HOME/~}\a"'
	PROMPT_COMMAND="history -a; $PROMPT_COMMAND" 
}

init_xirrus () {
	PROJ="$(basename $XIRRUS_ROOT)"

	PS1="\[$reset\][\[$green\]\h \[$cyan\]$PROJ \[$magenta\]\W\[$yellow\]\$(git branch 2>/dev/null | grep \* | sed 's/* \(.*\)/ (\1)/') \[$red\]\!\[$reset\]]\$ "
	[[ "$TERM" =~ "rxvt" ]] && PROMPT_COMMAND='echo -ne "\e]0;${USER}@${HOSTNAME%%.*}: $PROJ: $PWD\a"'

	[ -e "$XIRRUS_ROOT/.gitconfig" ] || ln -s "$HOME/.gitconfig" "$XIRRUS_ROOT/.gitconfig"

	CDPATH="${CDPATH#:}"
	if [ -d $XIRRUS_SANDBOX ]; then
		add_path CDPATH $XIRRUS_SANDBOX ""
	else
		add_path CDPATH $XIRRUS_ROOT ""
	fi
	export CDPATH
	export HOME="$XIRRUS_ROOT"

	alias xb="./xirrus_build"
	alias xbv="./xirrus_build -v"
	alias xbvi="./xirrus_build -v install"
}

fun  () { fortune -a | fmt -80 -s | cowsay -$(shuf -n 1 -e b d g p s t w y) -f $(shuf -n 1 -e $(cowsay -l | tail -n +2)) -n; }
vie  () { bin=$(which "$1") && vim "$bin"; }
cate () { bin=$(which "$1") && cat "$bin"; }

e () { "$@"     & exit        ; }
n () { "$@"     ; echo -e "\a"; }
t () { time "$@"; echo -e "\a"; }

which () {
	{ alias; declare -f; } | /usr/bin/which --tty-only --read-alias --read-functions --show-tilde --show-dot "$@"
}
# this is where it actually starts, might think of it as
# main()

add_path   PATH /sbin /usr/sbin /usr/local/sbin /bin /usr/bin /usr/local/bin $HOME/bin
add_path CDPATH $HOME/xds $HOME/projects $HOME/packages /var /usr /etc $HOME ""

export   PATH
export CDPATH

# if shell isn't interactive, exit at this point
[[ -z "$PS1" || ! "$-" =~ i ]] && return

fun

init_colors
init_misc
init_alias
init_prompt

[ -n "$XIRRUS_ROOT" ] && init_xirrus

# want the ls alias
cs () {
	cd $@
	ls
}

# vim: set ts=4 sw=4 noexpandtab: