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

Example completion script for the command foo

    local cur prev opts
    opts="--help --verbose --version"

    if [[ ${cur} == -* ]] ; then
        COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
        return 0
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 topic: HEPrc > MattVliet > MattsBashPage
Topic revision: r1 - 2011-01-18 - mvliet
This site is powered by the TWiki collaboration platform Powered by PerlCopyright © 2008-2020 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback