简体   繁体   English

bash选项卡如何完成工作?

[英]How does bash tab completion work?

I have been spending a lot of time in the shell lately and I'm wondering how the tab autocomplete works. 我最近花了很多时间在shell中,我想知道选项卡自动完成是如何工作的。 What's the mechanism behind it? 它背后的机制是什么? How does the bash know the contents of every directory? bash如何知道每个目录的内容?

There are two parts to the autocompletion: 自动完成有两个部分:

  • The readline library, as already mentioned by fixje, manages the command line editing, and calls back to bash when tab is pressed, to enable completion. 正如fixje已经提到的那样,readline库管理命令行编辑,并在按下tab时回调bash,以启用完成。 Bash then gives (see next point) a list of possible completions, and readline inserts as much characters as are identified unambiguously by the characters already typed in. (You can configure the readline library quite much, see the section Command line editing of the Bash manual for details.) Bash然后给出(参见下一点)可能的完成列表,readline插入尽可能多的字符,这些字符由已经输入的字符明确标识。(您可以配置readline库,请参阅Bash的命令行编辑部分)手册了解详情。)

  • Bash itself has the built-in complete to define a completion mechanism for individual commands. Bash本身具有内置的complete ,可以为各个命令定义完成机制。 If for the current command nothing is defined, it used completion by file name (using opendir/readdir, as Ignacio said). 如果对于当前命令没有定义任何内容,它使用文件名完成(使用opendir / readdir,如Ignacio所说)。

    The part to define your own completions is described in the section Programmable Completion . 定义您自己的完成的部分在可编程完成部分中描述。 In short, with complete «options» «command» you define the completion for some command. 简而言之,通过complete «options» «command»您可以定义某些命令的完成。 For example complete -u su says when completing an argument for the su command, search for users of the current system . 例如, complete -u su表示在完成su命令的参数时,搜索当前系统的用户

    If this is more complicated than the normal options can cover (eg different completions depending on argument index, or depending on previous arguments), you can use -F function , which will then invoke a shell function to generate the list of possible completions. 如果这比正常选项可以覆盖的更复杂(例如,取决于参数索引的不同完成,或取决于先前的参数),您可以使用-F function ,然后它将调用shell函数来生成可能的完成列表。 (This is used for example for the git completion, which is very complicated, depending on subcommand and sometimes on options given, and using sometimes names of branches (which are nothing bash knows about). (例如,这用于git完成,这非常复杂,取决于子命令,有时取决于给定的选项,并且有时使用分支的名称(这不是bash所知道的)。

You can list the existing completions defined in your current bash environment using simply complete , to have an impression on what is possible. 您可以使用简单的complete列出当前bash环境中定义的现有完成,以便对可能的内容留下印象。 If you have the bash-completion package installed (or however it is named on your system), completions for a lot of commands are installed, and as Wrikken said, /etc/bash_completion contains a bash script which is then often executed at shell startup to configure this. 如果您安装了bash-completion软件包(或者它在系统上命名),则会安装许多命令的完成,并且正如Wrikken所说, /etc/bash_completion包含一个bash脚本,然后经常在shell启动时执行配置这个。 Additional custom completion scripts may be placed in /etc/bash_completion.d ; 其他自定义完成脚本可以放在/etc/bash_completion.d ; those are all sourced from /etc/bash_completion . 这些都来自/etc/bash_completion

If you are interested in the basics: Bash uses readline which features history and basic completion. 如果您对基础知识感兴趣:Bash使用readline ,其中包含历史记录和基本完成功能。 You could inspect the source if you want to get a detailed understanding. 如果您想要详细了解,可以检查来源。 Furthermore, you can use readline to build your own CLI interfaces with completion 此外,您可以使用readline构建自己的CLI接口并完成

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM