aboutsummaryrefslogblamecommitdiff
path: root/bash/bashrc.d/cd.bash
blob: 9ccfce5706e47456f498c068ab4aa60c02f46824 (plain) (tree)
1
2
3
4
5
6

                                                                          
      

                 
                






                     
                                      




                     
        


                                                     
            

                                                              
                    
          
        
                                       


      
# 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 arg
    local -a opts
    for arg ; do
        case $arg in
            --)
                shift
                break
                ;;
            -*)
                shift
                opts[${#opts[@]}]=$arg
                ;;
            *)
                break
                ;;
        esac
    done
    if (($# == 2)) ; then
        if [[ $PWD == *"$1"* ]] ; then
            builtin cd "${opts[@]}" -- "${PWD/$1/$2}"
        else
            printf 'bash: %s: could not replace substring\n' \
                "$FUNCNAME" >&2
            return 2
        fi
    else
        builtin cd "${opts[@]}" -- "$@"
    fi
}