diff options
author | Tom Ryder <tom@sanctum.geek.nz> | 2013-11-04 17:47:56 +1300 |
---|---|---|
committer | Tom Ryder <tom@sanctum.geek.nz> | 2013-11-04 17:47:56 +1300 |
commit | 73f3223d6814f2e0e8534b9971f2dc62ab0565aa (patch) | |
tree | f8ce0e791e9dc783d2bd56302a12435d963bbb92 /bash/bashrc.d | |
parent | Add commentary to the cryptic GnuPG conf file (diff) | |
download | dotfiles-73f3223d6814f2e0e8534b9971f2dc62ab0565aa.tar.gz dotfiles-73f3223d6814f2e0e8534b9971f2dc62ab0565aa.zip |
More general approach to options for cd wrapper
Diffstat (limited to 'bash/bashrc.d')
-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 } |