Bash completion for cake files

Updated: 2012-08-19
Updated: 2012-06-18
Updated: 2012-04-06
Published: 2011-11-01

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

# Task name completion for cake files.
function _cake() {
    local cur tasks
    cur="${COMP_WORDS[COMP_CWORD]}"
    tasks=$(cake 2>/dev/null | 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 _cake cake c

Now it you type cake <Tab> at the bash command prompt you will get a list of tasks from the current Cakefile. 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 c alias for cake to ~/.bashrc:

alias c='cake'

This recipe was taken from An introduction to bash completion: part 2 and modified for cake.

See also my subsequent post Bash completion for Jake files.

3 Responses to “Bash completion for cake files”

  1. thomasfrossman Says:

    Great!
    I also did this small modification to supress stderr messages

    tasks=$(cake 2>/dev/null | awk ‘{print $2}’)

  2. srackham Says:

    Updated, thanks!

  3. sebaatrtbf Says:

    Thanks! I was looking for this.

Leave a reply to srackham Cancel reply