bash 补全可防止退格键

发布于 2024-09-19 03:57:10 字数 736 浏览 10 评论 0原文

我正在尝试为我编写的实用程序脚本设置 bash 完成,因此我将以下脚本添加到 /etc/bash_completion.d:

_mcd()
{
    local cur words
    COMPREPLY=()

    cur="${COMP_WORDS[COMP_CWORD]}"
    words=`mcd-completion-words`
    COMPREPLY=( $(compgen -W "${words}" -- "$cur") )
    return 0
}
complete -F _mcd mcd

mcd-completion-words 脚本以编程方式查找命令的可用运算符。当我重新启动 bash(或获取脚本)时,我可以成功使用 Tab 键完成,但如果这样做,我将无法再退格到已完成的字符之后。

另外,如果我尝试列出所有选项(例如,我尝试用制表符完成,但没有任何单词),bash 会在命令中添加一个制表符,而我也无法退格。

如何让 bash 模仿正常的文件完成行为?任何帮助表示赞赏。谢谢!


这是 mcd-completion-words 的简化测试用例,但仍然表现出相同的行为。奇怪的是,丹尼斯的案例也适用于我(例如,当替换为“一二三”时)。

#!/usr/bin/env php
<?php

print "one two three four five six seven eight nine";

I am trying to set up bash completion for a utility script I wrote, so I added the following script to /etc/bash_completion.d:

_mcd()
{
    local cur words
    COMPREPLY=()

    cur="${COMP_WORDS[COMP_CWORD]}"
    words=`mcd-completion-words`
    COMPREPLY=( $(compgen -W "${words}" -- "$cur") )
    return 0
}
complete -F _mcd mcd

The mcd-completion-words script programmatically finds available operators for the command. When I restart bash (or source the script), I can successfully tab complete, but if I do so, I can no longer backspace past a completed character.

Also, if I attempt to list all options (e.g. I attempt to tab complete w/ no word in place), bash adds a tab to the command, which I also cannot backspace.

How can I make bash mimic normal file completion behavior? Any help is appreciated. Thanks!


Here's a reduced testcase for mcd-completion-words that still exhibits the same behavior. Curiously, Dennis' case works for me as well (when substituting in words="one two three", for example).

#!/usr/bin/env php
<?php

print "one two three four five six seven eight nine";

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

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

发布评论

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

评论(1

枯叶蝶 2024-09-26 03:57:10

这是一个简化的测试用例;即使是空的“php”命令也会导致错误的完成行为:

$ _mcd() { php -r ""; COMPREPLY=( one ); }
$ complete -F _mcd mcd
$ mcd <TAB>            # Becomes 'mcd one' all right
$ mcd one <BACKSPACE>  # ERROR: doesn't work

这是在带有 bash-3.2.48/bash-4.0.33 和 bash-3.2.48/bash-4.0.33 的机器(Ubuntu)上。 php-5.2.6-3ubuntu4。在另一台机器(Debian)上有 bash-3.2.39 & php-5.2.6-1+lenny9,完成&退格键一切正常。

使用 perl -e "" 而不是 php -r "" 也可以。

不知道怎么回事,php与readline冲突?也许你可以尝试 bug-bash 邮件列表?

Here's a reduced test case; even an empty `php' command causes the erroneous completion behaviour:

$ _mcd() { php -r ""; COMPREPLY=( one ); }
$ complete -F _mcd mcd
$ mcd <TAB>            # Becomes 'mcd one' all right
$ mcd one <BACKSPACE>  # ERROR: doesn't work

This is on machine (Ubuntu) with both bash-3.2.48/bash-4.0.33 & php-5.2.6-3ubuntu4. On another machine (Debian) with bash-3.2.39 & php-5.2.6-1+lenny9, completion & backspace goes all right.

Using perl -e "" instead of php -r "" also goes all right.

Don't know what's going on, php conflicting with readline? Maybe you can try bug-bash mailing list?

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