How to programming TAB completion into bash functions, programs and other such things.
Minimum Requirements
Here are the minimum requirements for BASH tab completion.
Code:
# 'local' can only be used within a function, exclude it if not using a function
COMPREPLY=()
local cur="${COMP_WORDS[COMP_CWORD]}"
local opts="--whatever --tabbing --options --you --want"
COMPREPLY=($(compgen -W "${opts}" -- ${cur}))
Lastly, you need one more line after this somewhere in the file, usually after your function:
complete -F "function_name" -o "default" "function_name"Example
# Replace function_name with your function
A simple example with the current code:
Code:
function sdSlighty Advanced
{
# 'local' can only be used within a function, exclude it if not using a function
COMPREPLY=()
local cur="${COMP_WORDS[COMP_CWORD]}"
local opts="--whatever --tab --options --you --want to have!"
COMPREPLY=($(compgen -W "${opts}" -- ${cur}))
if [[ ! $* =~ \n ]]; then # Check command been submitted, no more tab-completing
echo "Command submitted"
... # more bash code, dealing with submitted command arguments
fi
}
complete -F "sd" -o "default" "sd"
# complete -F "sd" -o "nospace" "sd" ## Use this if you do not want a space after the completed word
You can manipulation the string input however you want, just make sure you clear COMPREPLY, and grab the current command prompt info, and append them using compgen.