Commandline Completion for Fabric

To get a list of functions in a Fabric file you use fab --list. I use Fabric frequently so I thought I'd save myself some time by using bash completion to render fab --list obsolete.

Being a learn-from-example type of person I immediately consulted Google for some precedence. After a bit of poking I ended up putting the following in /etc/bash_completion.d/fab.

    _fab(){
        COMPREPLY=()
        local word="${COMP_WORDS[COMP_CWORD]}"
        local completions="$(fab --list | egrep -v '(^Avail|^$)' | sed 's/^[ ]*//')"
        COMPREPLY=( $(compgen -W "$completions" "$word") )
    }

complete -F _fab fab

Now, I type fab hit tab twice and a list of Fabric functions appears and is completed, as if by magic, as I type.

I'll add to this document when my understanding of the COMP* functions and data structures improves...