Bash for Camel.CaseNames (cd CCN <tab> => cd Camel.CaseName)
I am looking for a way to implement completion for file / directory names of the form
Foo.Bar.Baz/
Foo.Bar.QuickBrown.Fox/
Foo.Bar.QuickBrown.Interface/
Foo.Bar.Query.Impl/
where completion will work like
~ $ cd QI<tab><tab>
Foo.Bar.QuickBrown.Interface/ Foo.Bar.Query.Impl/
~ $ cd QIm<tab><enter>
~/Foo.Bar.Query.Impl $
However, my simplified approach to creating a glob template from input (e.g. QIm→ *Q*I*m) does not exactly work for files / directories using the same prefix. In the above case, I get
~ $ 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/
i.e. bash replaces the current word with the longest common prefix for possible terminations, which in this case leads to a larger set of terminations.
Here is my completion function:
_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
}
Any tips on how to fix this without breaking the normal filling of the file / directory name?
+4
1 answer
. :
% 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>
+1