aboutsummaryrefslogtreecommitdiff
path: root/bash/bash_completion.d/bd.bash
blob: e67cdd097a908b3f86886b7632ddef7410fb1d90 (plain)
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
# Completion setup for bd()
_bd() {

    # Only makes sense for the first argument
    ((COMP_CWORD == 1)) || return

    # Build a list of dirnames in $PWD
    local -a dirnames
    IFS=/ read -rd '' -a dirnames < <(printf '%s\0' "${PWD#/}")

    # Remove the last element in the array (the current directory)
    ((${#dirnames[@]})) || return
    dirnames=("${dirnames[@]:0:${#dirnames[@]}-1}")

    # Add the matching dirnames to the reply
    local dirname
    for dirname in "${dirnames[@]}" ; do
        case $dirname in
            "${COMP_WORDS[COMP_CWORD]}"*)
                COMPREPLY[${#COMPREPLY[@]}]=$(printf %q "$dirname")
            ;;
        esac
    done
}
complete -F _bd bd