Friday, April 2, 2010

Ulimited "cd -" history

I use bash and I use "cd -" to go to the previous directory. To be able to go back more than one step, I had to replace the original cd command with a function:

# cd with automatic pushd
function cd() {
    if test "x$1" = "x-" ; then
        popd >/dev/null
    else
        pushd . >/dev/null
        builtin cd "$@"
    fi
}

Put that into your ~/.bashrc and start a new bash.

Example usage

/home$ cd /opt
/opt$ cd /var/log
/var/log$ cd -
/opt$ cd -
/home$

No comments: