bash 选项卡完成如何工作? [英] How does bash tab completion work?

查看:28
本文介绍了bash 选项卡完成如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

推荐答案

自动补全有两部分:

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

  • 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 editing of the Bash manual for details.)

Bash 本身具有内置的 complete 来定义单个命令的完成机制.如果当前命令未定义任何内容,则使用文件名补全(使用 opendir/readdir,正如 Ignacio 所说).

Bash itself has the built-in complete to 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).

定义您自己的完成的部分在 可编程完成.总之,与complete «options» «command» 你定义一些命令的完成.例如 complete -u su完成 su 命令的参数后,搜索当前系统的用户.

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 su says when completing an argument for the su command, search for users of the current system.

如果这比上面的更复杂正常选项可以涵盖(例如,根据参数索引或根据先前的参数不同的完成),您可以使用 -F 函数,然后它会调用一个 shell 函数来生成可能的补全列表.(这例如用于 git 完成,它非常复杂,取决于子命令,有时在给定的选项上,有时还使用分支的名称(bash 对此一无所知).

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).

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

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_completion contains 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.

这篇关于bash 选项卡完成如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆