bash 选项卡完成如何工作?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5570795/
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-09-09 20:22:40  来源:igfitidea点击:

How does bash tab completion work?

bashshellautocomplete

提问by CamelBlues

I have been spending a lot of time in the shell lately and I'm wondering how the tab autocomplete works. What's the mechanism behind it? How does the bash know the contents of every directory?

我最近在 shell 中花了很多时间,我想知道选项卡自动完成是如何工作的。它背后的机制是什么?bash 如何知道每个目录的内容?

采纳答案by Pa?lo Ebermann

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. 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 editingof the Bash manual for details.)

  • Bash itself has the built-in completeto define a completion mechanism for individual commands. If for the current command nothing is defined, it used completion by file name (using opendir/readdir, as Ignacio said).

    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. For example complete -u susays when completing an argument for the sucommand, search for users of the current system.

    If this is more complicated than the normal options can cover (e.g. 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. (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).

  • 正如 fixje 已经提到的,readline 库管理命令行编辑,并在按下 Tab 时回调 bash 以启用完成。然后 Bash 给出(见下一点)可能的补全列表,并且 readline 插入尽可能多的字符,这些字符由已经输入的字符明确标识。(您可以配置 readline 库,请参阅Bash 的命令行编辑部分详细手册。)

  • Bash 本身具有complete为单个命令定义完成机制的内置机制。如果当前命令没有定义任何内容,它使用文件名完成(使用 opendir/readdir,正如 Ignacio 所说)。

    定义您自己的完成的部分在Programmable Completion部分中描述。简而言之, complete ?options? ?command?您可以为某些命令定义完成。例如complete -u su当完成su命令的参数时,搜索当前系统的用户

    如果这比普通选项可以涵盖的更复杂(例如,根据参数索引或根据先前的参数不同的补全),您可以使用-F function,然后它会调用一个 shell 函数来生成可能的补全列表。(这例如用于 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. 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_completioncontains a bash script which is then often executed at shell startup to configure this. Additional custom completion scripts may be placed in /etc/bash_completion.d; those are all sourced from /etc/bash_completion.

您可以使用 simple 列出在当前 bash 环境中定义的现有完成complete,以了解可能的情况。如果你安装了 bash-completion 包(或者它在你的系统上是这样命名的),就会安装很多命令的补全,并且正如 Wrikken 所说,它/etc/bash_completion包含一个 bash 脚本,然后通常在 shell 启动时执行来配置它。可以将其他自定义完成脚本放置在/etc/bash_completion.d; 这些都来自/etc/bash_completion.

回答by fixje

If you are interested in the basics: Bash uses readlinewhich features history and basic completion. 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

如果您对基础知识感兴趣:Bash 使用具有历史记录和基本完成功能的readline。如果您想获得详细的了解,可以查看源代码。此外,您可以使用 readline 构建您自己的 CLI 接口并完成