bash 补全可以通过编程方式调用吗?
我想要的是一个可以从程序调用的函数,这样它就可以完成 bash 给出命令行和按下 TAB 的位置的方式。
. /etc/bash_completion
generate_completions "command arg1 arg2" 17
会返回相同的结果,因为
command arg1 arg2[TAB]
我还没有看到任何方法可以做到这一点。
What I want is a function I can call from a program so it completes the way bash would given a commandline and a location where TAB was pressed.
. /etc/bash_completion
generate_completions "command arg1 arg2" 17
would return the same thing as
command arg1 arg2[TAB]
I haven't seen any way to do this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
实际上,我必须这样做才能弄清楚 apt-get 自动完成功能如何在 ubuntu 上工作(构建了我自己的伪存储库工具:)
这是一个多步骤过程:
首先,
complete -p
将为您提供一个列表以一组命令形式的所有完成,您可以运行这些命令来复制配置。例如,假设您想要寻找 apt-get 的自动完成功能。然后:这告诉你 shell 函数 _apt_get 被完成机制调用。
您需要重新创建函数使用的特殊变量,即 COMP_LINE (整行)、COMP_WORDS (所有参数的 bash 数组 - 基本上拆分 COMP_LINE)、COMP_CWORD (索引,应指向最后一个值)、COMP_POINT (您正在执行自动完成的单词内的位置)和 COMP_TYPE (这是您告诉它您想要完成的方式,就像您点击 Tab 一样)。
注意:阅读联机帮助页以获取更多信息——这就是我首先想到的方法。
man bash
I actually had to do this to figure out how apt-get autocomplete works on ubuntu (built my own pseudo-repository tool :)
This is a multistep process:
First,
complete -p
will give you a listing of all completions in the form of a set of commands you can run to replicate the configuration. For example, lets say you want to hunt down the autocomplete forapt-get
. Then:This tells you that the shell function _apt_get is called by the completion mechanism.
You need to recreate the special variables used by the function,, namely COMP_LINE (the full line), COMP_WORDS (bash array of all of the arguments -- basically split COMP_LINE), COMP_CWORD (index, should point to last value), COMP_POINT (where within the word you are doing the autocomplete), and COMP_TYPE (this is how you tell it that you want to complete as if you hit tab).
Note: read the manpage for more info -- this is how i figured it out in the first place.
man bash