Bash completion for Grunt files

Published: 2012-09-11

I modified my cake bash completion script for Grunt.

All you need to do is add this completion function and command to your ~/.bashrc file:


# Task name completion for grunt files.
function _grunt() {
    local cur tasks
    cur=${COMP_WORDS[COMP_CWORD]}
    # Extract list of task names from help text.
    tasks="$(grunt --help --no-color | awk 'task==1 {print $1} /Available tasks/ {task=1} /^$/ {task=0}')"
    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 _grunt grunt g

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

alias g='grunt'
About these ads

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s


%d bloggers like this: