Tuesday, November 07, 2006

My Bash Profile

Bash is my favourite shell and here is my .bashrc which has grown substantially over the years as I have added (and will continue to add) more and more useful aliases and functions. These are very useful because they save time and also stop you from having to type in long error-prone commands.

My personal favourite alias is ll='ls -ltr' and I don't understand why some people still type in the long form for such a common command.



###############################
# FAHD SHARIFF'S BASH PROFILE #
###############################

HISTCONTROL=ignoredups
EDITOR=emacs

set -o notify
set -o braceexpand
set -o emacs

##########
# PROMPT #
##########

PS1=\[\e[0m\]\e]2;\u@\h:`tty`>\w[\d,\t]\a\n\[\e[4;34;1m\]\u@\h:\[\e[0;31;1m\]\w>\[\e[m\]

###############
# COMPLETIONS #
###############

shopt -s extglob progcomp cdspell

# Make directory commands see only directories
complete -d cd mkdir rmdir pushd jd

# Make file commands see only files
complete -f cat less more chown ln strip nedit emacs

complete -f -X '!*.@(zip|ZIP|jar|JAR|exe|EXE|pk3|war|wsz|ear|zargo|xpi)' unzip zipinfo
complete -f -X '*.Z' compress
complete -f -X '!*.@(Z|gz|tgz|Gz|dz)' gunzip zcmp zdiff zcat zegrep zfgrep zgrep zless zmore
complete -f -X '!*.Z' uncompress
complete -f -X '!*.@(gif|jp?(e)g|tif?(f)|pn[gm]|p[bgp]m|bmp|xpm|ico|xwd|tga|pcx|GIF|JP?(E)G|TIF?(F)|PN[GM]|P[BGP]M|BMP|XPM|ICO|XWD|TGA|PCX)' ee display
complete -f -X '!*.@(gif|jp?(e)g|tif?(f)|png|p[bgp]m|bmp|x[bp]m|rle|rgb|pcx|fits|pm|GIF|JPG|JP?(E)G|TIF?(F)|PNG|P[BGP]M|BMP|X[BP]M|RLE|RGB|PCX|FITS|PM)' xv qiv
complete -f -X '!*.@(ps|PS)' gv ggv
complete -f -X '!*.@(ps|PS|pdf|PDF)' fmerge
complete -f -X '!*.@(dvi|DVI)?(.@(gz|Z|bz2))' xdvi
complete -f -X '!*.@(dvi|DVI)' dvips dviselect dvitype
complete -f -X '!*.@(pdf|PDF)' acroread gpdf xpdf
complete -f -X '!*.texi*' makeinfo texi2html
complete -f -X '!*.@(?(la)tex|?(LA)TEX|texi|TEXI|dtx|DTX|ins|INS)' tex latex slitex jadetex pdfjadetex pdftex pdflatex texi2dvi
complete -f -X '!*.fig' xfig
complete -f -X '!*.@(?([xX]|[sS])[hH][tT][mM]?([lL]))' netscape mozilla lynx appletviewer hotjava
complete -f -X '!*.tar' tar
complete -f -X '!*.java' javac
complete -f -X '!*.idl' idl idlj

# user commands see only users
complete -u su usermod userdel passwd write groups w talk

# bg completes with stopped jobs
complete -A stopped -P '%' bg

# other job commands
complete -j -P '%' fg jobs disown

# readonly and unset complete with shell variables
complete -v readonly unset

# set completes with set options
complete -A setopt set

# shopt completes with shopt options
complete -A shopt shopt

# unalias completes with aliases
complete -a unalias

# type and which complete on commands
complete -c command type which

# complete hostnames
complete -A hostname ssh telnet rlogin ftp ping traceroute

###########
# ALIASES #
###########

alias ..=cd ..
alias ...=cd ../..
alias ....=cd ../../..
alias .....=cd ../../../..

alias cl=clear
alias cla=clear;la
alias cll=clear;ll
alias cls=clear;ls
alias clal=clear;lal

alias rmdir=rm -rf

alias d=date
alias ff=find . -name $1
alias h=history

alias l=ls
alias la=ls -a
alias ll=ls -ltr
alias lal=ls -al
alias ls=ls -F
alias sl=ls

alias more=less
alias mroe=more
alias m=more

alias r=fc -s
alias igrep=grep -i

#############
# FUNCTIONS #
#############

#kill a process by name
pskill()
{
if [ -z $1 ]; then
echo -e \e[0;31;1mUsage: pskill [processName]\e[m;
else
ps -au $USER | grep -i $1 |awk {print kill -9 $1}|sh
fi
}

#jump to a directory
jd()
{
if [ -z $1 ]; then
echo -e \e[0;31;1mUsage: jd [directory]\e[m;
else
findresults=( $(find . -type d -name $1) )
count=${#findresults[@]}
if [ $count = 1 ]; then
file=${findresults[0]}
cd $file
else
if [ $count = 0 ]; then
echo No such directory
else echo Ambiguous: $count directories found
fi
fi
unset findresults
unset count
fi
}

#display directory tree structure
tree()
{
echo -e \033[1;35m

(cd ${1-.} ; pwd)
find ${1-.} -print | sort -f | sed \
\
-e s,^${1-.},, \
-e /^$/d \
-e s,[^/]*/\([^/]*\)$,\ |-->\1, \
-e s,[^/]*/, | ,g

echo -e \033[0m
}

#mkdir and cd combined
mkcd()
{
if [ -z $1 ]; then
echo -e \e[0;31;1mUsage: mkcd [directory]\e[m;
else
if [ -d $1 ]; then
echo Changed to $1.;
cd $1;
else
mkdir $1;
echo Created $1;
cd $1;
fi;
fi
}

#######
# END #
#######

mesg -n
echo -e \e[0;31;1m$USER logged in to `tty` on `date`\e[m

3 comments:

  1. I commend you, this is a great bashrc! Mine is pretty well developed, but I definitely added quite a few new items from your examples!

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. Nice bashrc .. Thank You! I edited my bashrc and added a bunch of stuff from yours. Mine even looks better now. :)

    ReplyDelete

Note: Only a member of this blog may post a comment.