How to tell `complete` to fall-back to its default?

Viewed 226

I'm using the complete bash-builtinin to enable arbitrary auto-completion for my python script. I set it up with:

complete -C './script.py --compgen "$@"' ./script.py

Such that whenever bash attempts to auto-complete it invokes my script with a flag --compgen that tells my script to do auto completion. This works fine.

However I want to be able to fall-back to bash's default behaviour in some situations from within script.py. Is there a way to tell complete that it should rerun without calling my script and do its default-thing instead?

Basically, I want to avoid re-implementing file-name expansion, when referring to a file.

2 Answers

Try this in your Python code:

import os
# if failed:
os.system("bash -c 'compgen -o default'")

It turns out, the fallback of outputting the compgen from the python script, has the drawback that it is unescaped. So, if the user tries to complete a file-name like a/b/c d.txt the command line will change from:

./script.py a/b/c\ d.txt

to

./script.py a/b/c d.txt

after hitting tab. Other expansions break as well. The complete builtin does offer a fallback, however:

complete -C './script.py --compgen "$@"' -o default ./script.py
#                                        ^^^^^^^^^^

What this does is tell complete to try the -C portion first and if that part returns zero matches, it asks default completion, which then does file-name resolution etc.

I found that one can even force complete to not offer any suggestions by returning exactly the empty string as a suggestion. This prevents the fallback from being run, but it also does not count as an actual suggestion to complete, so it doesn't consider the empty string.

Related