How can I make my program use a tab?

I noticed that some programs (for example, hg) allow the user to tabulate individual parts of the command. For example, if in the working directory of the hg repository I type:

hg qpush --move b8<TAB>

He will try to execute the command with any mercury fixes in my patch queue that starts with "b8".

What I would like to do is to imitate this behavior in my program. That is, I have a number of commands that depend on files in a specific directory, and I would like to be able to provide a tab in the shell. Is there an API to provide this on Ubuntu Linux (preferably using python, as written in my script)?

+7
source share
4 answers

To do this, you need to write tab completion modules for your shell. The default shell is in most Linux bash distributions, so you should write a script termination (usually a shell script). After you have 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 Guide for writing your completion scripts . To use completion on a Mac, see https://trac.macports.org/wiki/howto/bash-completion .

For examples of completion files, see the bash-completion project from Debian (also on Github ). See also https://unix.stackexchange.com/questions/4738/an-easy-bash-completion-tutorial .

If you are using zsh , hack.augusto related documentation for writing completions .

+5
source

This is what readline is for.

In fact, readline is a common C library, so it has bindings in many languages. And I can say that I had a lot of fun.

Enjoy b)

+3
source

You might want to try the zsh shell, it has an excellent completion system with support for many applications.

The completion system is written with a shell language, but if you really want to use python, you can start the interpreter from your completion function. On the other hand, if you want to write a completion for your own software, you will need to do some reading ( user manual and manpage ).

+1
source

Take a look at the source of the cmd module in the Python library. It supports team completion.

+1
source

All Articles