【问题标题】:Bash completion for Camel.CaseNames (cd CCN<tab> => cd Camel.CaseName)Camel.Case 名称的 Bash 补全(cd CCN<tab> => cd Camel.Case 名称)
【发布时间】:2016-06-01 12:24:24
【问题描述】:

我正在寻找一种方法来实现此类文件/目录名称的补全

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 $

但是,我从输入(例如QIm -> *Q*I*m)构建全局模式的简单方法不适用于共享相同前缀的文件/目录。在上述情况下,我得到 ​​p>

~ $ 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
}

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

【问题讨论】:

    标签: bash bash-completion


    【解决方案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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-03
      • 2016-07-22
      • 2010-11-06
      • 2013-08-21
      相关资源
      最近更新 更多