The cd command is probably one of the most used Unix commands. It's intent is very straight forward: change to the directory given, or $HOME, if none is given. So what's wrong with it? Well, if you pass it a non-directory file, it just complains:

% cd org/wxwindows/
cd: not a directory: org/wxwindows/

That seems pretty straight forward, since it cannot possibly change directories to a file. I've thought it would be better if cd would change to the directory of the file, in this case org/wxwindows, rather than error.

Maybe I find this so useful because I've been doing so much Java lately. This modified cd makes it easy to grep and change directories, without having to manually delete the filename. I've also found this handy when using output from the locate or rpm -ql commands.

Anyhow, here's the zsh code:

cd_improved ()
    if (( ${#argv} == 0 )); then
    local dir=$1
    if [[ -f $dir ]]; then
        echo "Ignoring: $file"
    cd "$dir"
alias cd='cd_improved'