Difference: MattsBashPage ( vs. 1)

Revision 12011-01-18 - mvliet

Line: 1 to 1
Added:
>
>
META TOPICPARENT name="MattVliet"

Bash autocompletion

There is a good tutorial for this here.

This is documented in man bash but here is a quick overview

The following table lists useful bash variables for completion

Variable
<-- -->
Sorted ascending
Description
COMP_CWORD index into ${COMP_WORDS} of the word containing the current cursor position
COMP_KEY The key used to invoke the current completion function
COMP_LINE The current command line
COMP_POINT the index if the cursor position relative to the beginning of the current command
COMP_TYPE see bash manual
COMP_WORDS An array variable of individual words in the current command line

Example completion script for the command foo

_foo() 
{
    local cur prev opts
    COMPREPLY=()
    cur="${COMP_WORDS[COMP_CWORD]}"
    prev="${COMP_WORDS[COMP_CWORD-1]}"
    opts="--help --verbose --version"

    if [[ ${cur} == -* ]] ; then
        COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
        return 0
    fi
}
complete -F _foo foo

To use this script either source it in your bashrc file, or save it in /etc/bash_completion.d/ to have it automatically loaded.

-- MattVliet - 2011-01-18

 
This site is powered by the TWiki collaboration platform Powered by PerlCopyright © 2008-2019 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback