如何为 linux 命令实现选项卡完成?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9286792/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 04:34:47  来源:igfitidea点击:

How is tab completion implemented for linux commands?

linuxbashcommand-line

提问by Matt

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

我注意到有时命令可以通过选项卡完成。

e.g. the xm command in xen.

例如,xen 中的 xm 命令。

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

你输入 xm[space][tab] 并打印出有效的选项,它们是:

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?

如何在 Linux 中实现我自己的 tab 命令完成?

采纳答案by Cascabel

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

这是一个相当广泛的问题,但一般的想法是您使用 thecompgencompletebuiltin注册一些东西。它们都记录在手册中。上一节记录了可编程完成的一般主题,介绍了如何处理完成尝试。

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_completionas a way of extending the default completion.

有关大量示例,请参阅/etc/bash_completion,它提供了 bash 附带的所有默认补全(除了完全内置的内容,如文件名补全)。有关更多示例,请参阅/etc/bash_completion.d; 这些是/etc/bash_completion作为扩展默认完成的一种方式自动获取的。

回答by SiegeX

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

这是通过 shell 通过使用GNU Readline 库在以下情况下完成的bash

回答by mrb

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.

bash 的智能补全由一系列脚本化的 bash 函数处理。在 Debian,可能是 Ubuntu,也可能是其他 Linux 发行版上,您可以在/etc/bash_completion.d.

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

关于此机制的官方文档位于http://www.gnu.org/software/bash/manual/bash.html#Programmable-Completion