#!/usr/bin/env zsh ################################################################################ # # This file is autoloaded by .zshrc, and actually loaded when executed # ################################################################################ # cd to basedir of file, if trying to cd to file. This is really useful when # `compdef _path_files cd` is defined. # Idea from from https://github.com/mika/zsh-pony#smart-cd # But this is smarter, sice it works just like the builtin. cd () { local qflag if (( ARGC )) && [[ $1 == '-q' ]]; then qflag='-q' shift fi # if only one argument, it is *not* a directory, and it exists or its basedir exists # this matches any file (socket, fifo, etc) if (( ARGC == 1 )) && [[ ! -d "$1" && ( -e "$1" || -d "${1:h}" ) ]]; then [[ -z $qflag ]] && printf '%s\n' "Correcting $1 to ${1:h}" builtin cd $qflag ${1:h} else builtin cd $qflag $@ fi } # END OF FILE #################################################################