Bash completion for Jake files

Jun 18, 2012 · bash · jake

To implement bash(1) tab completion for Jakefile task names all you need to do is add this completion function and command to your ~/.bashrc file:

See also my previous post Bash completion for cake files.

# Task name completion for Jake files.
function _jake() {
    local cur tasks
    cur=${COMP_WORDS[COMP_CWORD]}
    # The sed command strips terminal escape sequences.
    tasks=$(jake -T \
            | sed -r 's/\x1b[[()=][;?0-9]*[0-9A-Za-z]?//g' \
            | awk '{print $2}')
    if [ $COMP_CWORD -eq 1 ]; then
        # Task name completion for first argument.
        COMPREPLY=( $(compgen -W "$tasks" $cur) )
    else
        # File name completion for other arguments.
        COMPREPLY=( $(compgen -f $cur) )
    fi
}
complete -o default -F _jake jake j

Now it you type jake <Tab> at the bash command prompt you will get a list of tasks from the current Jakefile. As with all bash completions, if you start typing a name and press Tab then bash will complete the name for you.

To cut down key strokes I’ve also added a j alias for jake to ~/.bashrc:

alias j='jake'
« Previous Next »