【问题标题】:Showing only the substrings of COMPREPLY bash completion options to the user仅向用户显示 COMPREPLY bash 完成选项的子字符串
【发布时间】:2011-04-26 11:35:11
【问题描述】:

在 bash 完成脚本中,假设 COMPREPLY=(aa/ba/ aa/bb/)。调用脚本时,完成选项对用户来说如下所示:

$ foo aa/b<TAB>
aa/ba/
aa/bb/

但是,我希望对这些选项的显示方式有更多的控制。特别是,我只想向用户显示每个 COMPREPLY 选项的子字符串,类似于现在 bash 中目录完成的工作方式:

$ foo aa/b<TAB>
ba/
bb/

有没有办法在 bash 中做到这一点?

【问题讨论】:

    标签: bash bash-completion


    【解决方案1】:

    我遇到了同样的问题,我通过调整将完成函数绑定到命令的方式来修复它。我知道这在您处理文件系统中的实际文件时有效,我认为它适用于任何类型的文件路径,如选项,但我不确定。

    之前:

    complete -F _fubar fubar
    

    之后:

    complete -o filenames -F _fubar fubar
    

    更多详情:Programmable Completion Builtins

    【讨论】:

    • 太棒了!现在我只需要弄清楚如何在我的函数中为不同的COMPREPLY 案例有选择地打开/关闭选项,不使用使用compopt
    【解决方案2】:

    这段取自 debian sid /etc/bash_completion 的代码应该会有所帮助:

    # Remove colon-word prefix from COMPREPLY items
    local colon_word=${1%${1##*:}}
    local i=${#COMPREPLY[*]}
    while [ $((--i)) -ge 0 ]; do
        COMPREPLY[$i]=${COMPREPLY[$i]#"$colon_word"}
    done
    

    【讨论】:

    • 感谢 enzotib。不幸的是,这种技术不起作用,因为子字符串替换了整个完成。也就是说,foo aa/b&lt;TAB&gt; 变为 foo ba/ :-(
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多