#/usr/bin/env bash _wiki() { ROOT=$HOME/git/wiki WORDS="list tree read edit peek search new remove sync push help" if [ "${#COMP_WORDS[@]}" = "2" ]; then # Command completion COMPREPLY=($(compgen -W "$WORDS" -- "${COMP_WORDS[1]}")) elif [ "${#COMP_WORDS[@]}" -ge "3" ]; then case "${COMP_WORDS[1]}" in new) # Special path/category completion for new page # creation. The idea is specify the categories of a new # page and they will converted into a path. # E.g.: # wiki new main cat sub page.md # creates # main/cat/sub/page.md local path i local -a toks for i in $(seq 2 $((${#COMP_WORDS[@]}-2))) do path="$path/${COMP_WORDS[i]}" done toks=($(compgen -d -- "$ROOT$path/${COMP_WORDS[${#COMP_WORDS[@]}-1]}")) for tok in "${toks[@]}" do # `${tok##*/}` is a Bash-ism to remove everything # until the last `/` (included). COMPREPLY+=(${tok##*/}) done ;; *) ;; esac else return fi } complete -F _wiki wiki