How is tab completion implemented for linux commands?

12,254

Solution 1

This is a pretty broad question, but the general idea is that you register something with the either the compgen or complete builtin. They're both documented in the manual. The previous section documents the general topic of programmable completion, going through how completion attempts are processed.

For a whole ton of examples, see /etc/bash_completion, which provides all the default completion that comes with bash (beyond the totally built-in stuff like filename completion). For even more examples, see anything in /etc/bash_completion.d; those are automatically sourced by /etc/bash_completion as a way of extending the default completion.

Solution 2

bash's smart completion is handled by a series of scripted bash functions. On Debian, probably Ubuntu, and maybe other Linux distributions, you can find your system's installed completions in /etc/bash_completion.d.

The official documentation on this mechanism is at http://www.gnu.org/software/bash/manual/bash.html#Programmable-Completion

Solution 3

This is done via the shell through the use of the GNU Readline library in the case of bash

Share:
12,254
hookenz
Author by

hookenz

Updated on June 13, 2022

Comments

  • hookenz
    hookenz almost 2 years

    I've noticed that sometimes commands can be tab completed.

    e.g. the xm command in xen.

    you type xm[space][tab] and it prints out the valid options which are:

    addlabel        destroy         info            network-attach  resume          sysrq           vnet-delete
    block-attach    dmesg           labels          network-detach  rmlabel         top             vnet-list
    block-detach    domid           list            network-list    save            trigger         vtpm-list
    block-list      domname         loadpolicy      new             sched-credit    unpause         
    cfgbootpolicy   dry-run         log             pause           sched-sedf      uptime          
    console         dump-core       makepolicy      reboot          serve           vcpu-list       
    create          dumppolicy      mem-max         rename          shutdown        vcpu-pin        
    debug-keys      getlabel        mem-set         resources       start           vcpu-set        
    delete          help            migrate         restore         suspend         vnet-create 
    

    That's pretty slick!

    How can I implement my own tab command completion in Linux?

  • Cascabel
    Cascabel about 12 years
    When half of your answer is a link to another question, that's usually a sign you should be voting to close as a duplicate, not posting an answer. (If you feel the second link is helpful, you can contribute it somewhere on that question.)
  • icyrock.com
    icyrock.com about 12 years
    @Jefromi Fair enough - I thought they were not the same, given the other question doesn't contain the request to actually do the completion, just how it works part. The second link is what I think refers to the second part.
  • Cascabel
    Cascabel about 12 years
    Yeah, it's not an exact duplicate, but I think it's close enough; it points you in the right direction, especially given that the answer mentions /etc/bash_completion, where you can find a zillion examples. If you want to add an answer to the other question focusing more on implementing actual completion stuff I think that'd be valuable.
  • icyrock.com
    icyrock.com about 12 years
    @Jefromi You've conviced me! :)