views:

194

answers:

2

In bash, I believe it is possible to enable tab completion on the terminal for terms that are specific to the executable being invoked.

For example, given an executable "eat" with valid arguments {cake, carrot, banana}, typing 'eat car' should complete to 'eat carrot'.

I believe this is possible because I have seen it with 'ant' tab-completing its targets (though how this was set up I don't know).

How can this behaviour be implemented?

A: 

This is quite similar to filename globbing where the shell will attempt to autocomplete based on the globbing wildcard...for instance....

echo foo*

will list all files in the current directory beginning with 'foo'...the bash shell globbed the wildcard and expanded it into a list of files...

MSDOS had a similar concept, although it was not explicitly linked in at run-time, I'm talking about the old Turbo C stuff, when the wildcard globbing was activated by linking with 'wildargs.obj' (if my memory serves me correct), internally, that code will iterate through the directory and expand the list based on wildcard pattern matching.

In Linux/*nix land, globbing is standard, but however, you cannot manually hit the sequence Tab key to do the pattern matching or completion...as different terminals may translate the tab key differently and of course handle it differently...

tommieb75
No, sorry, this is not what the OP is asking about at all.
Dennis Williamson
+4  A: 

This is done with scripts in /etc/bash_completion.d/ and if you want to write your own completion support for an executable, here's a tutorial to get you started.

If you only need to get the behaviour working for common executables, your Linux distro probably has a bash-completion package available with support for common commands.

Martin