Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

zshrc: smarter smarter cd #142

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 18 additions & 5 deletions etc/zsh/zshrc
Original file line number Diff line number Diff line change
Expand Up @@ -3470,14 +3470,27 @@ function cl () {
cd $1 && ls -a
}

# smart cd function, allows switching to /etc when running 'cd /etc/fstab'
# smart cd function, allows switching to /etc when running 'cd /etc/fstab',
# `cd /etc/fst` or `cd /etc/*tab`
function cd () {
if (( ${#argv} == 1 )) && [[ -f ${1} ]]; then
[[ ! -e ${1:h} ]] && return 1
print "Correcting ${1} to ${1:h}"
# zsh's builtin cd has multiple switches and behaviors, see zshbuiltins(1)
if builtin cd "$@"; then
:
# we bail out if options were passed to cd and the builtin failed
elif [[ $1 =~ '[+-].*' ]]; then
return 1
# if cd failed and it was not given weird flags, it might actually be that
# we need to intervene
# builtin cd fails with too many arguments
elif [[ -d ${1} ]]; then
print "Correcting $@ to $1"
builtin cd ${1}
# we help correct a typo or cd-ing to not a dir by looking at the parent
elif [[ -d ${1:h} ]]; then
print "Correcting $@ to ${1:h}" >&2
builtin cd ${1:h}
else
builtin cd "$@"
return 1
fi
}

Expand Down