Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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..

share|improve this question

2 Answers 2

up vote 1 down vote accepted

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

compgen -o filenames -A file ...
complete -o filenames -A file
share|improve this answer

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
COMPREPLY=()
return 0

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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