# .bashrc # Configuration file for bash (Bourne-again shell). # Much of the content comes from # http://tldp.org/LDP/abs/html/sample-bashrc.html # with some help from # http://www.ibm.com/developerworks/linux/library/l-tip-prompt/ # Additionally, I have my own custom additions. Some of the custom stuff follows my # .tcshrc file. # TODO If you upgrade systems, don't forget to check /etc/bashrc and maybe copy # over it with etc_bashrc_pt00 to use .. to go up a directory, to have nice # prompt, etc. test -s ~/.alias && . ~/.alias || true # Source global definitions. if [ -f /etc/bashrc ]; then . /etc/bashrc fi # Read extended tab completion file. if [ -f ~/.bash_completion.sh ]; then . ~/.bash_completion.sh fi #------------------------------------------------------------- # Automatic setting of $DISPLAY (if not set already) # This works for linux - your mileage may vary.... # The problem is that different types of terminals give # different answers to 'who am i'...... # I have not found a 'universal' method yet #------------------------------------------------------------- function get_xserver () { case $TERM in xterm ) XSERVER=$(who am i | awk '{print $NF}' | tr -d ')''(' ) # Ane-Pieter Wieringa suggests the following alternative: # I_AM=$(who am i) # SERVER=${I_AM#*(} # SERVER=${SERVER%*)} XSERVER=${XSERVER%%:*} ;; aterm | rxvt) # find some code that works here..... ;; esac } if [ -z ${DISPLAY:=""} ]; then get_xserver if [[ -z ${XSERVER} || ${XSERVER} == $(hostname) || ${XSERVER} == "unix" ]]; then DISPLAY=":0.0" # Display on local host else DISPLAY=${XSERVER}:0.0 # Display on remote host fi fi export DISPLAY #--------------- # Some settings #--------------- ulimit -S -c 0 # Don't store core files. set -o notify # Report terminated BG jobs immdediately. set -o noclobber # Do not overwrite existing file when redirection is used. set -o ignoreeof # Ctrl-D does not exit the shell; must type "exit". set -o nounset # Error if expansion is attempted on an unset variable. #set -o xtrace # Useful for debugging # Enable options: shopt -s cdable_vars shopt -s cdspell # Correct minor spelling errors when 'cd'ing shopt -s checkhash shopt -s checkwinsize # Check window size after each command. shopt -s cmdhist shopt -s extglob # Necessary for programmable completion. shopt -s histappend histreedit histverify shopt -s mailwarn #shopt -s progcomp shopt -s no_empty_cmd_completion # bash>=2.04 only shopt -s sourcepath # Disable options: shopt -u mailwarn unset MAILCHECK # I don't want my shell to warn me of incoming mail export TIMEFORMAT=$'\nreal %3R\tuser %3U\tsys %3S\tpcpu %P\n' export HISTIGNORE="&:bg:fg:ll:h" export HOSTFILE=$HOME/.hosts # Put a list of remote hosts in ~/.hosts #----------------------- # Greeting, motd etc... #----------------------- # Define some colors first: Black='\e[0;30m' Dark_Gray='\e[1;30m' Blue='\e[0;34m' Light_Blue='\e[1;34m' Green='\e[0;32m' Light_Green='\e[1;32m' Cyan='\e[0;36m' Light_Cyan='\e[1;36m' Red='\e[0;31m' Light_Red='\e[1;31m' Purple='\e[0;35m' Light_Purple='\e[1;35m' Brown='\e[0;33m' Yellow='\e[1;33m' Light_Gray='\e[0;37m' White='\e[1;37m' No_Color='\e[0m' # Looks best on a black background..... echo -e "${Light_Cyan}This is BASH ${Light_Red}${BASH_VERSION%.*} \ ${Light_Cyan} - DISPLAY on ${Light_Red}$DISPLAY${White}" #echo -e ${WHITE} "hi\n" date if [ -x /usr/games/fortune ]; then /usr/games/fortune -s # makes our day a bit more fun.... :-) fi function _exit() # function to run upon exit of shell { echo -e "${Light_Red}Goodbye${No_Color}" } trap _exit EXIT #--------------- # Shell Prompt #--------------- #if [[ "${DISPLAY#$HOST}" != ":0.0" && "${DISPLAY}" != ":0" ]]; then #HILIT=${Red} # remote machine: prompt will be partly red #else #HILIT=${Cyan} # local machine: prompt will be partly cyan #fi # --> Replace instances of \W with \w in prompt functions below #+ --> to get display of full path name. # Set my prompt. # \u = username, \h = hostname, \w = full dir path, \W = current directory # Remember to put \[ and \] around the colors; otherwise, line wrapping is horrible. function my_prompt() { # Add full dir path. PS1="\w$ " #PS1="\u@\H:\w$ " # Add username@host for SSH convenience. return unset PROMPT_COMMAND # Put the full current directory in the title. #local my_title_bar="\e]2;\w\a" local my_title_bar="\e]2;\w [\u@\h]\a" case $TERM in *term | rxvt ) # PS1="\[$my_title_bar\]\[${HILIT}\][\u@\h] \[${Light_Cyan}\]\W> \[${White}\]" ;; # PS1="\[$my_title_bar\]\[${Light_Cyan}\]\W> \[${White}\]" ;; #PS1="\[$my_title_bar\]\[${Light_Cyan}\]\w> \[${White}\]" ;; PS1="\u@\H:\w $ " ;; #PS1="\[\e[0;31m\]\u\[\e[0m\]\[\e[1;31m\]@\[\e[0m\]\[\e[0;31m\]\H\[\e[0m\] \w $ " #PS1="\[\u@H \w $ ";; linux ) #PS1="\[${HILIT}\][\u@\h] \[${Light_Cyan}\]\w> \[${White}\]" ;; PS1="\u@\H:\w $ " ;; *) #PS1="[\u@\h] \w > " ;; PS1="\u@\H:\w $ " ;; esac } my_prompt # set colors tcsh uses for `ls` -- regular==white: directory==light blue: # link==yellow: c, c++, and java source files and c and c++ header files==red #setenv LS_COLORS "no=1;37:fi=1;37:di=1;36:ln=1;33:*.cpp=1;31:*.cc=1;31:*.c=1;31:*.h=1;31:*.hpp=1;31:*.java=1;31" export LS_COLORS="no=1;37:fi=1;37:di=1;36:ln=1;33" # Colors for Mac: 'man ls' and search for this variable. export LSCOLORS="fxfxcxdxbxfgfdabagacad" #------------------- # Personnal Aliases #------------------- # Prevent accidentally clobbering files. alias rm='rm -ri' alias RMF='/bin/rm -rf' #alias rm='~/bin/safe_rm' alias cp='cp -RiL' alias mv='mv -i' alias clstring="echo \`/bin/date\` Wade Spires ''" # Create subdirectories along the path. alias mkdir='mkdir -p' alias mntusb='/home/wade/bin/usb_mount' alias umntusb='sync;umount /mnt/usb' # ls fun alias la='ls -Al' # show hidden files alias ls='ls -hFG' # add colors for filetype recognition alias lx='ls -lXB' # sort by extension alias lk='ls -lSr' # sort by size alias lc='ls -lcr' # sort by change time alias lu='ls -lur' # sort by access time alias lr='ls -lR' # recursive ls alias lt='ls -ltr' # sort by date alias lm='ls -al |more' # pipe through 'more' alias tree='tree -CsuF' # nice alternative to 'ls' # Directory stack manipulation. alias p=pushd alias P=popd alias pp='pushd +1' alias ks='kuickshow' alias xine='xine -G 1000x1000 -l --no-splash' alias h='history' alias XT='exit' # Tailoring 'less'. alias more='less' export PAGER=less export LESSCHARSET='latin1' export LESSOPEN='|/usr/bin/lesspipe.sh %s 2>&-' # Use this if lesspipe.sh exists. # No line numbers. Quit if only 1 page. No error bells. #export LESS='-n -F -Q -x2' export LESS='-n -i -w -J -z-4 -g -e -M -X -F -R -P%t?f%f \ :stdin .?pb%pb\%:?lbLine %lb:?bbByte %bb:-...' #alias top='xtitle Processes on $HOST && top' #alias make='xtitle Making $(basename $PWD) ; make' #alias ncftp="xtitle ncFTP ; ncftp" function lll() { ls -l "$@"| egrep "^d" ; ls -lXB "$@" 2>&-| egrep -v "^d|total "; } function linerange() { if [ -z "$3" ]; then echo 'Print $2 lines starting at line $1 from file $3, i.e., lines in the range [$1,$1 + $2).' return fi tail +`echo $1+1 | bc` $3 | head -n $2 } # .. and functions #function man () #{ #for i ; do # xtitle The $(basename $1|tr -d .[:digit:]) manual #command man -a "$i" #done #} # cd into a directory and ls it. function cl() { cd $1; ls; } #----------------------------------- # File & strings related functions: #----------------------------------- # Find a file with a pattern in name. function ff() { /usr/bin/find . -type f -iname '*'$*'*' -ls ; } # Find a file with pattern $1 in name and execute command $2 on it. function fe() { /usr/bin/find . -type f -iname '*'$1'*' -exec "${2:-file}" {} \; ; } # Find pattern in a set of files and highlight them. function fstr() { OPTIND=1 local case="" local usage="fstr: find string in files. Usage: fstr [-i] \"pattern\" [\"filename pattern\"] " while getopts :it opt do case "$opt" in i) case="-i " ;; *) echo "$usage"; return;; esac done shift $(( $OPTIND - 1 )) if [ "$#" -lt 1 ]; then echo "$usage" return; fi local SMSO=$(tput smso) local RMSO=$(tput rmso) /usr/bin/find . -type f -name "${2:-*}" -print0 | xargs -0 grep -sn ${case} "$1" 2>&- | \ sed "s/$1/${SMSO}\0${RMSO}/gI" | more } # Cut last n lines in file, 10 by default. function cuttail() { nlines=${2:-10} sed -n -e :a -e "1,${nlines}!{P;N;D;};N;ba" $1 } # Move filenames to lowercase. function lowercase() { for file ; do filename=${file##*/} case "$filename" in */*) dirname==${file%/*} ;; *) dirname=.;; esac nf=$(echo $filename | tr A-Z a-z) newname="${dirname}/${nf}" if [ "$nf" != "$filename" ]; then mv "$file" "$newname" echo "lowercase: $file --> $newname" else echo "lowercase: $file not changed." fi done } # Swap 2 file names around. function swap() { local TMPFILE=tmp.$$ mv "$1" $TMPFILE mv "$2" "$1" mv $TMPFILE "$2" } #----------------------------------- # Process/system related functions: #----------------------------------- function my_ps() { ps $@ -u $USER -o pid,%cpu,%mem,bsdtime,command ; } function pp() { my_ps f | awk '!/awk/ && $0~var' var=${1:-".*"} ; } # This function is roughly the same as 'killall' on linux # but has no equivalent (that I know of) on Solaris function killps() # kill by process name { local pid pname sig="-TERM" # default signal if [ "$#" -lt 1 ] || [ "$#" -gt 2 ]; then echo "Usage: killps [-SIGNAL] pattern" return; fi if [ $# = 2 ]; then sig=$1 ; fi for pid in $(my_ps| awk '!/awk/ && $0~pat { print $1 }' pat=${!#} ) ; do pname=$(my_ps | awk '$1~var { print $5 }' var=$pid ) if ask "Kill process $pid <$pname> with signal $sig?" then kill $sig $pid fi done } # Get IP adresses. Used by ii(). function my_ip() { MY_IP=$(/sbin/ifconfig eth1 | awk '/inet/ { print $2 } ' | \ sed -e s/addr://) MY_ISP=$(/sbin/ifconfig eth1 | awk '/P-t-P/ { print $3 } ' | \ sed -e s/P-t-P://) } # Get current host related info. function ii() { echo -e "\nYou are logged on ${Light_Red}$HOST" echo -e "\nAdditionnal information:$No_Color " ; uname -a echo -e "\n${Light_Red}Users logged on:$No_Color " ; w -h echo -e "\n${Light_Red}Current date :$No_Color " ; date echo -e "\n${Light_Red}Machine stats :$No_Color " ; uptime echo -e "\n${Light_Red}Memory stats :$No_Color " ; free my_ip 2>&- ; echo -e "\n${Light_Red}Local IP Address :$No_Color" ; echo ${MY_IP:-"Not connected"} echo -e "\n${Light_Red}ISP Address :$No_Color" ; echo ${MY_ISP:-"Not connected"} echo } # Misc. utilities: # Repeat n times command. function repeat() { local i max max=$1; shift; for ((i=1; i <= max ; i++)); do # --> C-like syntax eval "$@"; done } # Get answer to yes/no question. function ask() { echo -n "$@" '[y/n] ' ; read ans case "$ans" in y*|Y*) return 0 ;; *) return 1 ;; esac } export EDITOR=vim export PYDOC=/usr/share/doc/packages/python/html export PYTHONDOCS=/usr/share/doc/packages/python/html export dbg='debug=1 release=0 optimize=0' alias grep='egrep --color=auto' alias sc='xargs | sed "s/ /,/g"' alias cs='xargs | sed "s/,/ /g"' alias sp='xargs | sed "s/ /|/g"' alias vi=vim alias pc=pychecker # Aliases for lazy and clumsy fingers. alias mroe=more alias m=make alias c=clear alias les=less alias chomod=chmod alias chomd=chmod alias x='chmod +x' alias ks='kuickshow' alias sl=ls alias m=make # New KDE terminal -- start in current directory alias nt='konsole --workdir=`pwd` &' # New Konqueror file manager -- start in current directory alias nf='kfmclient exec .' # Remote login shortcuts. #alias lap-ssh='ssh wade@192.168.1.103' # Remote ftp shortcuts. #alias sftp-cet 'sftp spires@cetus4.cs.utk.edu' alias bc='bc -l' # Use floating-point for calculator. # Use vi for command-line editing. #set -o vi # Change the caps-lock key to the escape key (for faster vi skillz). # Repeated calls result in an error, so we just redirect output to /dev/null. # This is a hack, but changing xorg.conf or messing with xkb doesn't seem to do # anything, possibly because gconf has taken over keyboard preferences (see # $HOME/.gconf/desktop/gnome/peripherals/keyboard/xkb/%gconf.xml). #/usr/X11R6/bin/xmodmap ~/.caps-to-esc >& /dev/null #/usr/bin/xmodmap /home/wade/.caps-to-esc >& /dev/null alias caps_to_esc='/usr/bin/xmodmap /home/wade/.caps-to-esc' # No bell, visible or audible. set bell-style none PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:/usr/X11R6/bin:/usr/X11R6/lib:/home/wade/java/SUNWappserver/bin:/home/wade/java/SUNWappserver/jdk/bin:/usr/kerberos/bin:/home/wade/bin:/home/wade/bin/qt/bin:. export CVS_RSH=ssh #export SHELL /bin/tcsh #export PAGER=less #export EDITOR=/usr/bin/vim export PKG_CONFIG_PATH=/usr/lib64/pkgconfig/ #export MORE='-c -s -d' #export LESS='-s -M' # Set Qt environment. #export QTDIR=/usr/local/qt #export QTDIR='/usr/lib64/qt-3.3' #export QTDIR='/opt/Qt-4.1.4' #export QTLIB=${QTDIR}/lib #export QTINC=${QTDIR}/include #export LD_LIBRARY_PATH=$QTDIR/lib #set hostname = `uname -n` #export HAVE_64BIT_TARGET #export HAVE_QT_3_3_INSTALLED # Set java environment. export CLASSPATH=.:..:~/ #export JAVA_BINDIR=/opt/jdk/jdk1.5.0_05/bin #export JAVA_HOME=/usr/bin/java #export JPATH=$JAVA_BINDIR:$path #export JPATH=$JAVA_BINDIR #======================================================================= # # PROGRAMMABLE COMPLETION - ONLY SINCE BASH-2.04 # Most are taken from the bash 2.05 documentation and from Ian McDonalds # 'Bash completion' package # (http://www.caliban.org/bash/index.shtml#completion) # You will in fact need bash-2.05a for some features # #======================================================================= if [ "${BASH_VERSION%.*}" \< "2.05" ]; then echo "You will need to upgrade to version 2.05 \ for programmable completion" return fi shopt -s extglob # necessary set +o nounset # otherwise some completions will fail complete -A hostname rsh rcp telnet rlogin r ftp ping disk complete -A export printenv complete -A variable export local readonly unset complete -A enabled builtin complete -A alias alias unalias complete -A function function complete -A user su mail finger complete -A helptopic help # currently same as builtins complete -A shopt shopt complete -A stopped -P '%' bg complete -A job -P '%' fg jobs disown complete -A directory mkdir rmdir complete -A directory -o default cd complete -A directory cl # Compression complete -f -o default -X '*.+(zip|ZIP)' zip complete -f -o default -X '!*.+(zip|ZIP)' unzip complete -f -o default -X '*.+(z|Z)' compress complete -f -o default -X '!*.+(z|Z)' uncompress complete -f -o default -X '*.+(gz|GZ)' gzip complete -f -o default -X '!*.+(gz|GZ)' gunzip complete -f -o default -X '*.+(bz2|BZ2)' bzip2 complete -f -o default -X '!*.+(bz2|BZ2)' bunzip2 # Postscript,pdf,dvi..... complete -f -o default -X '!*.ps' gs ghostview ps2pdf ps2ascii complete -f -o default -X '!*.dvi' dvips dvipdf xdvi dviselect dvitype complete -f -o default -X '!*.pdf' acroread pdf2ps complete -f -o default -X '!*.+(pdf|ps)' gv complete -f -o default -X '!*.texi*' makeinfo texi2dvi texi2html texi2pdf complete -f -o default -X '!*.tex' tex latex slitex complete -f -o default -X '!*.lyx' lyx complete -f -o default -X '!*.+(htm*|HTM*)' lynx html2ps # Multimedia complete -f -o default -X '!*.+(jp*g|gif|xpm|png|bmp)' xv gimp complete -f -o default -X '!*.+(mp3|MP3)' mpg123 mpg321 complete -f -o default -X '!*.+(ogg|OGG)' ogg123 complete -f -o default -X '!*.pl' perl perl5 # This is a 'universal' completion function - it works when commands have # a so-called 'long options' mode , ie: 'ls --all' instead of 'ls -a' _get_longopts () { $1 --help | sed -e '/--/!d' -e 's/.*--\([^[:space:].,]*\).*/--\1/'| \ grep ^"$2" |sort -u ; } _longopts_func () { case "${2:-*}" in -*) ;; *) return ;; esac case "$1" in \~*) eval cmd="$1" ;; *) cmd="$1" ;; esac COMPREPLY=( $(_get_longopts ${1} ${2} ) ) } complete -o default -F _longopts_func configure bash complete -o default -F _longopts_func wget id info a2ps ls recode _make_targets () { local mdef makef gcmd cur prev i COMPREPLY=() cur=${COMP_WORDS[COMP_CWORD]} prev=${COMP_WORDS[COMP_CWORD-1]} # if prev argument is -f, return possible filename completions. # we could be a little smarter here and return matches against # `makefile Makefile *.mk', whatever exists case "$prev" in -*f) COMPREPLY=( $(compgen -f $cur ) ); return 0;; esac # if we want an option, return the possible posix options case "$cur" in -) COMPREPLY=(-e -f -i -k -n -p -q -r -S -s -t); return 0;; esac # make reads `makefile' before `Makefile' if [ -f makefile ]; then mdef=makefile elif [ -f Makefile ]; then mdef=Makefile else mdef=*.mk # local convention fi # before we scan for targets, see if a makefile name was specified # with -f for (( i=0; i < ${#COMP_WORDS[@]}; i++ )); do if [[ ${COMP_WORDS[i]} == -*f ]]; then eval makef=${COMP_WORDS[i+1]} # eval for tilde expansion break fi done [ -z "$makef" ] && makef=$mdef # if we have a partial word to complete, restrict completions to # matches of that word if [ -n "$2" ]; then gcmd='grep "^$2"' ; else gcmd=cat ; fi # if we don't want to use *.mk, we can take out the cat and use # test -f $makef and input redirection COMPREPLY=( $(cat $makef 2>/dev/null | \ awk 'BEGIN {FS=":"} /^[^.# ][^=]*:/ {print $1}' \ | tr -s ' ' '\012' | sort -u | eval $gcmd ) ) } complete -F _make_targets -X '+($*|*.[cho])' make gmake pmake # cvs(1) completion _cvs () { local cur prev COMPREPLY=() cur=${COMP_WORDS[COMP_CWORD]} prev=${COMP_WORDS[COMP_CWORD-1]} if [ $COMP_CWORD -eq 1 ] || [ "${prev:0:1}" = "-" ]; then COMPREPLY=( $( compgen -W 'add admin checkout commit diff \ export history import log rdiff release remove rtag status \ tag update' $cur )) else COMPREPLY=( $( compgen -f $cur )) fi return 0 } complete -F _cvs cvs _killall () { local cur prev COMPREPLY=() cur=${COMP_WORDS[COMP_CWORD]} # get a list of processes (the first sed evaluation # takes care of swapped out processes, the second # takes care of getting the basename of the process) COMPREPLY=( $( /bin/ps -u $USER -o comm | \ sed -e '1,1d' -e 's#[]\[]##g' -e 's#^.*/##'| \ awk '{if ($0 ~ /^'$cur'/) print $0}' )) return 0 } complete -F _killall killall killps # A meta-command completion function for commands like sudo(8), which # need to first complete on a command, # then complete according to that command's own # completion definition - currently not quite foolproof # (e.g. mount and umount don't work properly), # but still quite useful -- # By Ian McDonald, modified by me. _my_command() { local cur func cline cspec COMPREPLY=() cur=${COMP_WORDS[COMP_CWORD]} if [ $COMP_CWORD = 1 ]; then COMPREPLY=( $( compgen -c $cur ) ) elif complete -p ${COMP_WORDS[1]} &>/dev/null; then cspec=$( complete -p ${COMP_WORDS[1]} ) if [ "${cspec%%-F *}" != "${cspec}" ]; then # complete -F # # COMP_CWORD and COMP_WORDS() are not read-only, # so we can set them before handing off to regular # completion routine # set current token number to 1 less than now COMP_CWORD=$(( $COMP_CWORD - 1 )) # get function name func=${cspec#*-F } func=${func%% *} # get current command line minus initial command cline="${COMP_LINE#$1 }" # split current command line tokens into array COMP_WORDS=( $cline ) $func $cline elif [ "${cspec#*-[abcdefgjkvu]}" != "" ]; then # complete -[abcdefgjkvu] #func=$( echo $cspec | sed -e 's/^.*\(-[abcdefgjkvu]\).*$/\1/' ) func=$( echo $cspec | sed -e 's/^complete//' -e 's/[^ ]*$//' ) COMPREPLY=( $( eval compgen $func $cur ) ) elif [ "${cspec#*-A}" != "$cspec" ]; then # complete -A func=${cspec#*-A } func=${func%% *} COMPREPLY=( $( compgen -A $func $cur ) ) fi else COMPREPLY=( $( compgen -f $cur ) ) fi } complete -o default -F _my_command nohup exec eval \ trace truss strace sotruss gdb complete -o default -F _my_command command type which man nice # Local Variables: # mode:shell-script # sh-shell:bash # End: