I'm trying to get a bash completion script set up and having some trouble.
I would like to set it up so the completions listed are either files matching a particular extension, or directories (which may or may not contain files of that extension).
The trouble I'm having is that the only way I can get the completions to contain files and directories is by using something like -o plusdirs -f -X '!*.txt'
, but when I let bash complete one of the directories, it just adds a space onto the end, rather than a slash.
_xyz()
{
local cur=${COMP_WORDS[COMP_CWORD]}
local prev=${COMP_WORDS[COMP_CWORD-1]}
#COMPREPLY=( $( compgen -f -X '!*.txt' -- $cur ) )
#COMPREPLY=( $( compgen -f -G '*.txt' -- $cur ) )
#COMPREPLY=( $( compgen -o filenames -f -X '!*.txt' -- $cur ) )
#COMPREPLY=( $( compgen -o dirnames -f -X '!*.txt' -- $cur ) )
COMPREPLY=( $( compgen -o plusdirs -f -X '!*.txt' -- $cur ) )
return 0
}
complete -F _xyz xyz
I've tried all the commented-out lines too, but they don't even expand the directories.
For testing, I've been running this in a directory with one .txt file and one directory "dir" (with a .txt file inside it, though that doesn't matter yet). Typing xyz <TAB>
with this function lists the directory and the .txt file, but typing xyz d<TAB>
expands to xyz dir
(well, with a space after "dir").