Skip to content
trans edited this page Dec 19, 2012 · 2 revisions

Tab Completion

Executable has built-in support for tab completion. The trick is to end a command line with an underscore. This will cause it to spit out a list of known possible completions. For example the yaml command from the yaml_command project does this:

  $ yaml _
  get set sort splat slurp edit view --data --debug --file --force --help --inspect --json --mute --yaml -F -f -h -j -m -y

So all you have to do is create a shell completion script that taps into that capability. No worries. We've already got some working examples below.

Using Bash

Here is an example of creating a tab completion script in bash for the a yaml command that uses Executable. This same basic template can work for any Executable-based command. Just substitute the name for yaml.

_yaml()
{
    local cur=${COMP_WORDS[COMP_CWORD]}
    local pos=(COMP_CWORD - 1)
    local pre=${COMP_WORDS[@]:0:$pos}
    local cpl=$($pre _)

    if [[ "$cur" == -* ]]; then
        cpl=${cpl[@]//^[^-]/}
    else
        cpl=${cpl[@]//-*/}
    fi

    COMPREPLY=( $(compgen -W "$cpl" -- $cur) )
}
complete -F _yaml yaml

ZSH

TODO: Anyone?

Clone this wiki locally