As part of a larger autocomplete function I'm writing, I want to use compgen to generate a list of files. I read the bash manual entries for compgen and complete, and from there I assumed that the option -G "*" would be the solution. I could not get it to work, though: The list of files in the current directory was shown, regardless of my input, i.e.:

$ cmd <Tab>
aa bb cc
$ cmd a<Tab>
aa bb cc
$ cmd aa<Tab>
aa bb cc

Therefore, I tried to debug this by using complete, which supports the same options as compgen, but I got the same result:

$ complete -G "*" cmd
$ cmd a<Tab>
aa bb cc

I also tried complete -o filenames, but this doesn't work either..

  • 2
    From what I can find, -G is pretty much useless, since it always returns what the glob matches but never filters against what you've typed so far. Anyplace you think you want to use -G, the answer seems to be to use -A file -X '!<glob>' – Edward Falk Jun 27 '16 at 22:11

I found the answer myself: I have to use the -A action option:

compgen -o filenames -A file ...
complete -o filenames -A file

Alternatively you can use the default autocompletion for filenames with

# at the top of the function to disable default
compopt +o default

# and where you need filename completion, re-enable it and send empty COMPREPLY
compopt -o default
return 0

based on https://stackoverflow.com/a/19062943/1817610

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.