aboutsummaryrefslogtreecommitdiff
path: root/bash/bash_completion.d/sd.bash
blob: 66dea73b8a4872bac2ff4e5c6d0221401225afdc (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
# Load _completion_ignore_case helper function
if ! declare -F _completion_ignore_case >/dev/null ; then
    source "$HOME"/.bash_completion.d/_completion_ignore_case.bash
fi

# Completion function for sd; any sibling directories, excluding the self
_sd() {

    # Build list of matching sibling directories
    local ci comp
    while IFS= read -d / -r comp ; do
        COMPREPLY[ci++]=$comp
    done < <(

        # Make globs expand appropriately
        shopt -s dotglob nullglob
        if _completion_ignore_case ; then
            shopt -s nocaseglob
        fi

        # Print matching sibling dirs that are not the current dir
        for sibling in ../"$2"*/ ; do
            sibling=${sibling%/}
            sibling=${sibling#../}
            case $sibling in
                ("${PWD##*/}") ;;
                (*) printf '%q/' "${sibling}" ;;
            esac
        done
    )
}
complete -F _sd sd