8

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?

hookenz
  • 36,432
  • 45
  • 177
  • 286
  • This is done by the shell. What shell are you asking about? (I'm guessing bash, since it's default in most distros now.) – Cascabel Feb 15 '12 at 01:39
  • yes bash, and I've now tagged my question with bash. Is it some kind of wrapper script? – hookenz Feb 15 '12 at 01:40
  • 2
    Looks like a dupicate of http://stackoverflow.com/questions/5570795/how-does-bash-tab-completion-work – Corey Farwell Feb 15 '12 at 01:44
  • Thanks @Corey Farwell, I didn't realize it was part of bash so the other question you link to provides me the answer. – hookenz Feb 15 '12 at 01:48

4 Answers4

10

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.

Cascabel
  • 479,068
  • 72
  • 370
  • 318
1

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

SiegeX
  • 135,741
  • 24
  • 144
  • 154
1

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

mrb
  • 3,281
  • 1
  • 20
  • 31
-1

See this:

and this:

Community
  • 1
  • 1
icyrock.com
  • 27,952
  • 4
  • 66
  • 85
  • 1
    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.) – Cascabel Feb 15 '12 at 01:53
  • @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. – icyrock.com Feb 15 '12 at 02:02
  • 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. – Cascabel Feb 15 '12 at 02:07
  • @Jefromi You've conviced me! :) – icyrock.com Feb 15 '12 at 02:18