I have two difficulties with compgen shell builtin.
I try show it in simple bash _filedir-like (using ls) code:
_myfiledir(){
path="$cur"
prefix=`echo /$path | grep -o "[0-9a-Z\-+_/]*/"`
sufix=`echo /$path | grep "/[0-9a-Z\-+_]*$" | grep -o "[0-9a-Z\-+_]*$"`
res=`ls -p $prefix`
COMPREPLY=($( compgen -o nospace -W "$res" -- $sufix ))
}
In this case when cur=="usr/li" then prefix=="/usr/" and sufix=="li"
I have two difficulties. With space and replacement. For example:
$ script usr/li[TAB]
I get:
$ script lib/ <- additional space here
I need:
$ script usr/lib/ <- no space here
This code is only for example.
-o nospace
This option don't work in
compgencontext. Usecompoptto change options. In your casecompopt -o nospaceusefull for whitespace issue, andcompopt -o filenamesfor filenames. Be careful with file names, you need add$prefixto$resvariable. Optionfilenamesonly cut part of full path when displayed to user for autocompletition.