Bash completion for Jake files

Published: 2012-06-18

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
    # 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) )
        # File name completion for other arguments.
        COMPREPLY=( $(compgen -f $cur) )
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'

One Response to “Bash completion for Jake files”

  1. justincalleja Says:

    cool thx! Make sure you’re using gnu sed.

Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: