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 |
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 |