diff options
-rw-r--r-- | bash/bashrc.d/cd.bash | 17 |
1 files changed, 11 insertions, 6 deletions
diff --git a/bash/bashrc.d/cd.bash b/bash/bashrc.d/cd.bash index 45537aeb..72a3fe0b 100644 --- a/bash/bashrc.d/cd.bash +++ b/bash/bashrc.d/cd.bash @@ -1,12 +1,17 @@ # If given two arguments to cd, replace the first with the second in $PWD, # emulating a Zsh function that I often find useful; preserves options too cd() { - local opt OPTIND=0 - local -a opts - while getopts elP opt; do - opts=("${opts[@]}" -"$opt") + local arg dir endopts + local -a opts dirs + for arg in "$@"; do + if [[ $arg == -- ]]; then + endopts=1 + elif [[ $arg == -* ]] && ! ((endopts)); then + opts=("${opts[@]}" "$arg") + else + dirs=("${dirs[@]}" "$arg") + fi done - shift $((OPTIND-1)) if (($# == 2)); then if [[ $PWD == *"$1"* ]]; then builtin cd "${opts[@]}" "${PWD/$1/$2}" @@ -15,7 +20,7 @@ cd() { return 1 fi else - builtin cd "${opts[@]}" "$@" + builtin cd "${opts[@]}" "${dirs[@]}" fi } |