1

I want a function that programmatically returns completion options from either bash or zsh. There are lots of examples of related questions on stackoverflow but no proper, generic answers anywhere. I do NOT want to know how to write a specific completer function for bash.

I've already tried implementing this by reading debian /etc/completion shell code, by echoing control-codes for tab into "bash -i", and even tried using automated subprocess interaction with python-pexpect. Every time I thought I was successful, I find some small problem that invalidates the whole solution. I'd accept a solution in any language, but ideally it would be python. Obviously the exact input output would vary depending on systems, but take a look at the example I/O below:

  • function("git lo") returns ["log","lol","lola"]
  • function("apt-get inst") returns ["apt-get install"]
  • function("apt-get") returns []
  • function("apt-get ") returns ["apt-get autoclean","apt-get autoremove", ...]
  • function ("./setup") returns ["./setup.py"]

If you are thinking of a solution written in shell, it would ideally be something I can execute without "source"ing. For instance bash "compgen" command looks interesting (try "compgen -F _git"), but note that "bash -c 'compgen -F _git'" does not work because the completion helper "_git" is not in scope.

Community
  • 1
  • 1
mvr
  • 611
  • 6
  • 5
  • 1
    In `bash`, completion is handled by `readline`, I'd start from the `READLINE` section in `man bash`, maybe deep diving in its source code... Ciao from – gboffi Nov 05 '14 at 16:00

1 Answers1

1

This gist is my best solution so far. It meets all the requirements, works well for multiple versions of bash on multiple OS's but it requires a subprocess call and it's so complicated it's absurd. The comments includes full documentation of all the outrageous slings and arrows. I'm still hoping for something more reasonable to come along, but unless it does.. this is it!

mvr
  • 611
  • 6
  • 5
  • I've come across the same problem. Your solution looks interesting. The best I've managed to come up with is printing the *results* of tab completion, i.e. if "foo pi" could complete to one of "pine", "pineapple" and "pinecone", then it would return those three words in a list ... where I actually want to see "foo pine", which is what would happen in the Bash shell when you hit tab. Here is the reference anyway if it still helps: https://stackoverflow.com/a/9505024/1907765. Does your solution work? – Lou Jan 14 '21 at 17:26