Camel.CaseNames的Bash完成(cd CCN <tab> =&gt; cd Camel.CaseName)

时间:2016-06-01 12:24:25

标签: bash bash-completion

我正在寻找一种方法来实现类型

的文件/目录名称的完成
Foo.Bar.Baz/
Foo.Bar.QuickBrown.Fox/
Foo.Bar.QuickBrown.Interface/
Foo.Bar.Query.Impl/

完成将像

一样工作
~ $ cd QI<tab><tab>
Foo.Bar.QuickBrown.Interface/    Foo.Bar.Query.Impl/
~ $ cd QIm<tab><enter>
~/Foo.Bar.Query.Impl $

但是,我从输入构建glob模式的简单方法(例如QIm - &gt; *Q*I*m)并不完全适用于共享相同前缀的文件/目录。在上面的例子中,我得到了

~ $ cd QI<tab><tab>
Foo.Bar.QuickBrown.Interface/    Foo.Bar.Query.Impl/
~ $ cd Foo.Bar.Qu<tab><tab>
Foo.Bar.QuickBrown.Fox/  Foo.Bar.QuickBrown.Interface/  Foo.Bar.Query.Impl/

即。 bash用可能的完成的最长公共前缀替换当前单词,在这种情况下导致更大的完成集。

这是我的完成功能:

_camel_case_complete()
{
    local cur pat
    COMPREPLY=()
    cur="${COMP_WORDS[COMP_CWORD]}"
    pat=$(sed -e 's/\([A-Z]\)/*\1*/g' -e 's/\*\+/*/g' <<< "$cur")
    COMPREPLY=( $(compgen -G "${pat}" -- $cur ) )
    return 0
}

任何提示如何在不破坏正常文件名/目录完成的情况下解决这个问题?

1 个答案:

答案 0 :(得分:1)

参见以下示例:

% ls -l
total 20
-rw-r--r-- 1 root root  315 2016-06-02 18:30 compspec
drwxr-xr-x 2 root root 4096 2016-06-02 17:56 Foo.Bar.Baz
drwxr-xr-x 2 root root 4096 2016-06-02 17:56 Foo.Bar.Query.Impl
drwxr-xr-x 2 root root 4096 2016-06-02 17:56 Foo.Bar.QuickBrown.Fox
drwxr-xr-x 2 root root 4096 2016-06-02 17:56 Foo.Bar.QuickBrown.Interface
% cat compspec
_camel_case_complete()
{
    local cur=$2
    local pat

    pat=$(sed -e 's/[A-Z]/*&/g' -e 's/$/*/' -e 's/\*\+/*/g' <<< "$cur")
    COMPREPLY=( $(compgen -G "${pat}" ) )
    if [[ ${#COMPREPLY[@]} -gt 1 ]]; then
        # Or use " " instead of "__"
        COMPREPLY[${#COMPREPLY[@]}]="__"
    fi

    return 0
}

complete -F _camel_case_complete cd
% . ./compspec
% cd QI<TAB><TAB>
__                            Foo.Bar.QuickBrown.Interface
Foo.Bar.Query.Impl
% cd QIm<TAB>
% cd Foo.Bar.Query.Impl<SPACE>
相关问题