Reputation: 6209
I've noticed that some programs (e.g. hg) allow the user to tab-complete specific parts of the command. For example, if, in an hg repository working directory, I type:
hg qpush --move b8<TAB>
It will try to complete the command with any mercurial patches in my patch queue that start with "b8".
What I'd like to do is imitate this behavior in my program. That is, I have a series of commands that depend on files within a certain directory, and I'd like to be able to provide tab completion in the shell. Is there an API for providing this on Ubuntu Linux (preferably using python, as that's what my script is written in)?
Upvotes: 11
Views: 4648
Reputation: 9
Take a look at the source of the 'cmd' module in the Python library. It supports command completion.
Upvotes: 0
Reputation: 30647
To do this, you need to write tab-completion modules for your shell. The default shell in most Linux distributions is bash
, so you should write a completion script (typically a shell script). Once you've written your script, add it to /etc/bash_completion.d/
. This should be distributed with your program (for Linux distributions, included in the package).
Debian Administration has a guide for writing your completion scripts. For using completion on a Mac, see https://trac.macports.org/wiki/howto/bash-completion.
For examples of completion files, take a look at the bash-completion
project from Debian (also on Github). See also https://unix.stackexchange.com/questions/4738/an-easy-bash-completion-tutorial.
If you use zsh
, hack.augusto linked to the documentation for writing completions.
Upvotes: 12
Reputation: 2170
You might want to try the zsh
shell, it has a great completion system with support for tons of applications.
The completion system is written with the shell language, but if you really want to use python you can run the interpreter from inside your completion function. The down side it that if you want to write completion for your own software, you will need to do some reading (user manual and the manpage for instance).
Upvotes: 0