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

                                                                          
      

                      
                             
                                   
        
                       
                        
                                     
                                                  
            
                                                                   

                    
        
                                    


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