I'm trying to setup autocompletion for a command, which takes only one parameter, a folder name,
In /secure/kernel_builds/
, I have the three following subfolder:
3.5.6/ 3.6.2/ source/
Now I want auto completion, that lists all sub folder name possible, but not the one named source
, right now I'm using a pretty stupid way to do it, by checking if the completion word is started with a number,
_avail_kernel_builds()
{
case $COMP_CWORD in
1)
if [[ "${COMP_WORDS[COMP_CWORD]}" =~ ^[0-9] ]];then
COMPREPLY=( /secure/kernel_builds/"${COMP_WORDS[COMP_CWORD]}"* )
else
COMPREPLY=( /secure/kernel_builds/"${COMP_WORDS[COMP_CWORD]}"[0-9]*/ )
fi
COMPREPLY=( "${COMPREPLY[@]#/secure/kernel_builds/}" )
COMPREPLY=( "${COMPREPLY[@]%/}" )
;;
esac
}
Is there a better way of doing so?
Best Answer
The
compgen
builtin has a-X
argument for this very purpose:So you could probably just do something like:
So
compgen -d
says "list the folders in the current directory", and-X "source"
says "...but filter out anything named "source".