有多少种方法可以在部分子字符串上获得 Bash 别名补全?

发布于 2024-07-13 02:07:50 字数 741 浏览 9 评论 0原文

问题:我有一个问题,Stack 上这个已提出的 Bash 补全问题显然没有回答溢出。 问题是,如何在部分子字符串上获得 Bash 别名补全(对于任何别名)。

示例: 例如,假设我有以下别名:

open.alicehome="cd /usr/home/alice"
open.bakerhome="cd /usr/home/baker"
open.charliehome="cd /usr/home/charlie"
gohomenow="shutdown -now"

我想懒洋洋地只需键入“baker{{TAB}}”来调用第二个别名。

我想懒洋洋地只需键入“home{{TAB}}”即可获取上述所有别名的列表,然后我可以使用键盘进行选择(最佳),或者通过键入区分三个选项的明确子字符串来进行选择(低于最佳)。

.. 或 ..

我想懒洋洋地只输入“home”,然后重复按 {{TAB}} 直到出现我想要的特定别名,然后按 {{ENTER}} 执行它。

尽情发挥创意: 如果你有办法做到这一点,即使需要诉诸极端的大师黑客技术,也请随时分享它,并随时向一个五岁孩子的水平解释你的大师黑客技术,他将不得不尝试实施你自己的想法。

欢迎链接到现有网页或 RTFM。

Question: I have a question that is apparently not answered by this already-asked Bash completion question on Stack Overflow. The question is, how to get Bash alias completion (for any alias) on a partial substring.

Example:
For example, assume I have the following aliases:

open.alicehome="cd /usr/home/alice"
open.bakerhome="cd /usr/home/baker"
open.charliehome="cd /usr/home/charlie"
gohomenow="shutdown -now"

I would like to lazily just type "baker{{TAB}}" to invoke the second alias.

I would like to lazily just type "home{{TAB}}" to get a list of all of the above aliases that I can then choose from with the keyboard (optimal) or choose by typing an unambiguous substring that distinguishes among the three options (less than optimal).

.. OR ..

I would like to lazily just type "home" and then repeatedly press {{TAB}} until the specific alias I want shows up, then press {{ENTER}} to execute it.

Feel free to be creative:
If you have a way to do this, even if it requires resorting to extreme guru hackery, please feel free to share it and feel free to explain your guru hackery to the level of a five-year-old who will have to try to implement your idea for himself.

Links to existing web-pages or RTFMs are welcome.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

給妳壹絲溫柔 2024-07-20 02:07:51

您可以使用 compgen -c -X'!*substring*' 生成指定子字符串的匹配项。

要将其包含在 bash 自动完成中,您可以在主目录中创建一个 .bash_completion 文件,其中包含如下内容:

_comp() {
    local cur

    cur=${COMP_WORDS[$COMP_CWORD]}

    COMPREPLY=( $( compgen -c -X '!*'$cur'*' ) )

}
complete -F _comp $nospace eval

当您在 eval 命令的参数上按 时,将运行此匹配函数。 我还没有找到一种方法来为 bash 中的第一个命令搜索包含新的匹配函数,我只找到了有关如何为特定命令添加自定义完成的文档,例如上面的 eval

但我有点怀疑这有多有用......没有简单的方法可以从列表中选择您想要的匹配。

You can generate the matches for a specified substring with compgen -c -X'!*substring*'

To include this in bash autocompletion you can create a .bash_completion file in your home directory containing something like this:

_comp() {
    local cur

    cur=${COMP_WORDS[$COMP_CWORD]}

    COMPREPLY=( $( compgen -c -X '!*'$cur'*' ) )

}
complete -F _comp $nospace eval

This match function will run when you press <TAB> on the arguments to the eval command. I haven't found a way to include a new matching function for the first command search in bash, I've only found documentation on how to add custom completions for a specific command, like eval above.

But I'm a bit skeptical to how useful this is... There is no easy way to select which match you want from the list.

萌梦深 2024-07-20 02:07:51

也许您可以通过自定义 bash_completion 设置来实现您想要的目标,但我不会为您完成这项工作,好吗? :-)

如果您不知道我在说什么,请参阅此处 和此处了解详细信息。 顺便说一句,我会觉得这很烦人

我想懒洋洋地只是打字
“home”然后反复按
{{TAB}} 直到特定别名 I
想要出现,然后按 {{ENTER}}
执行它。

我认为这是M$风格的“完成”(你是来DOS/Windows环境的吗?)

Probably you can achieve what you want with custom bash_completion settings, but I'm not going to do the job for you, ok? :-)

If you don't know what I'm talking about, see here and here for details. By the way, I would find very annoying this

I would like to lazily just type
"home" and then repeatedly press
{{TAB}} until the specific alias I
want shows up, then press {{ENTER}} to
execute it.

which I think is the M$ style of "completion" (are you coming for DOS/windows environment?)

乖不如嘢 2024-07-20 02:07:50

如果您确实想更改 bash 的选项卡行为,

# ~/.inputrc
# The default binding for [Tab] is ``complete'', which is what we're used to.
# ``menu-complete'' gives you irssi-like behavior: cycle through completions.
"\t": menu-complete

请参阅 GNU Readline 库 # 让 Readline 为您输入

If you really want to change bash's tab behavior,

# ~/.inputrc
# The default binding for [Tab] is ``complete'', which is what we're used to.
# ``menu-complete'' gives you irssi-like behavior: cycle through completions.
"\t": menu-complete

This is documented in The GNU Readline Library # Letting Readline Type For You.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文