aboutsummaryrefslogtreecommitdiff
path: root/bash/bashrc.d/ud.bash
blob: c83b7c5696e8b8184648de24de3ac3035c648723 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# Shortcut to step up the directory tree with an arbitrary number of steps,
# like cd .., cd ../.., etc
ud() {

    # Check and save optional first argument, number of steps upward; default
    # to 1 if absent
    local -i steps=${1:-1}
    if ! ((steps > 0)) ; then
        printf 'bash: %s: Invalid step count %s\n' "$FUNCNAME" "$1" >&2
        return 2
    fi

    # Check and save optional second argument, target directory; default to
    # $PWD (typical usage case)
    local dir=${2:-$PWD}
    if [[ ! -e "$dir" ]] ; then
        printf 'bash: %s: Target dir %s does not exist\n' "$FUNCNAME" "$2" >&2
        return 1
    fi

    # Append /.. to the target the specified number of times
    local -i i
    for (( i = 0 ; i < steps ; i++ )) ; do
        dir=${dir%/}/..
    done

    # Try to change into it
    cd -- "$dir"
}

# Completion is only useful for the second argument
_ud() {
    if ((COMP_CWORD == 2)) ; then
        local word=${COMP_WORDS[COMP_CWORD]}
        COMPREPLY=( $(compgen -A directory -- "$word" ) )
    else
        return 1
    fi
}
complete -F _ud ud