Wednesday, 7 November 2007

Directory hopping in the terminal...

Tried of hopping a around in your favorite bash/sh shell and typing the full directory every time? At least I did ;-) Put the code attached below in your $HOME/.profile and start using quick jumps :-)

Make a mark of a current directory using `mark `. And go back by it using `back `. As the directory mapping is shared throughout all your open terminal hopping around will never be the same again!

Happy hopping!


markBackCommon()
{
MARKFILE="$HOME/.mark"
DIR=`pwd -P`
if [ -z "$1" ]; then
MARK=B
else
MARK=$1
fi
}

mark()
{
markBackCommon $1
grep -v -e '^$MARK#' -e '^B#' $MARKFILE > $MARKFILE.tmp
(cat $MARKFILE.tmp; echo "$MARK#$DIR"; echo "B#$DIR") | sort -u > $MARKFILE
}

back()
{
markBackCommon $1
TARGET=`awk -F# "/^$MARK#/ {print \\$2}" $MARKFILE`

if [ -n "$TARGET" ]; then
cd $TARGET
fi
}

listmark()
{
markBackCommon 0
cat $MARKFILE
}

alias m=mark
alias b=back
alias lm=listmark

No comments:

Post a Comment