[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: Command arguments autocompletion



On Tue, Oct 13, 2009 at 02:24:17PM +0200, Matteo Riva wrote:
> I noticed that in a shell I can use tab-complete even on command
> arguments, as in:
> 
>   $ aptitude sa<TAB>
> 
> will expand to
> 
>   $ aptitude safe-upgrade
> 
> How does this work?  Where does bash look for possible completion
> candidates?  Has it always been like this or is this a recently new
> feature (I could swear that this didn't work in the past)?

The code for that is through programmable bash completion:

  /etc/bash_completion      - the base "scriptlet"
  /etc/bash_completion.d/*  - extensions by various packages

/etc/bash_completion is sourced into the shell on each interactive
shell. See /etc/bash.bashrc .

See the 'complete' command in the bash documentation. Generally the
completion functions are a good example at just how unreadable shell
code is.

For instance for aptitude it defines a function called '_aptitude'
(which is an arbitrary name. by convention: _${command_name}), and
eventually runs:

  complete -F _aptitude $default aptitude

which makes the function _aptitude the completion function for the
command 'aptitude'.

And if you don't like it, don't use it:

tzafrir@sweetmorn:~$ time . /etc/bash_completion

real    0m0.313s
user    0m0.252s
sys     0m0.040s

-- 
Tzafrir Cohen         | tzafrir@jabber.org | VIM is
http://tzafrir.org.il |                    | a Mutt's
tzafrir@cohens.org.il |                    |  best
ICQ# 16849754         |                    | friend


Reply to: