How can zsh be configured to autocomplete directory name with camelcase matching?

9

1

Eclipse and Resharper have a nice way of navigating files.

If I have the following folders (or files) in my current directory

MyReallyLongName
MyReallyLongAndFunName
MyReallyLongAndNotReallyFunName

In eclipse I can navigate to them by using only the capitalized letters. Correspondingly in zsh I want to be able to type:

cd MRLAFN(Tab)(Enter)

To get into MyReallyLongAndFunName in one step

If I want to do that with zsh right now, I have to type My(Tab)Fun(Tab)(Enter)

Rohit Manokaran

Posted 2012-02-28T05:14:57.577

Reputation: 93

Answers

6

This works for me:

zstyle ':completion:*' matcher-list 'r:[^A-Z0-9]||[A-Z0-9]=** r:|=*' 

Then I can do this in a test directory:

touch MyReallyLongName MyReallyLongAndFunName MyReallyLongAndNotReallyFunName
ls MRLANRFN<TAB>
ls MyReallyLongAndNotReallyFunName

I cobbled it together from tips in the Zshell User's Guide.

phord

Posted 2012-02-28T05:14:57.577

Reputation: 176

add it to your ~/.zshrc – Cody – 2017-07-07T16:55:17.727

This doesn't quite work as expected, with AbcAde AdFg HlAkAd, ls AA<TAB> completes to Ad, while I'd expect AbcAde. – arekolek – 2017-10-11T08:57:59.617

@arekolek "Ad" is a partial completion. If you hit TAB again (depending on your zsh configuration) you will see all the files that match. It should include all three files. Then you can choose one with the menu selection or by typing some more of the filename to guide the completer. – phord – 2017-10-11T23:55:41.120

@arekolek Sorry - I read that wrong. I'm not sure how you got to Ad from there. I thought it was a partial-ambiguous match, but it's hard to tell from your example. Does a 2nd TAB produce any more results? – phord – 2017-10-12T00:05:38.750

Can you please describe where I have to place this extension (file, folder)? You might also be interested in adding it to the zsh-completions project.

– JJD – 2014-06-12T15:46:34.957

2

I got this to work "quite well" by adding a file _camel_case to my personal ~/.zsh/functions folder (take any folder that is in your $fpath variable) with the following content:

#autoload

[[ -z "$PREFIX" ]] && return 1

relpath=$(dirname $PREFIX)

[[ -e $relpath ]] || return 1

files=$(ls $relpath)

regex=$(echo $(basename $PREFIX) | sed -e 's/\([A-Z][^A-Z]*\)/\1[^A-Z]+/g')

correctedfiles=($(echo $files | grep -P $regex | sed -e :a -e '$!N;s/\n/ /;ta'))

results=($(for file in $correctedfiles; do echo "$relpath/$file"; done))

compadd -U -f -- $results

Then, I added the following line to my ~/.zshrc

zstyle ':completion:*' completer _complete _correct _path_files _camel_case

Note that this is most probably not a "good" solution as I do not have too much knowledge of the completion system of zsh (only few people really have, I guess), but it does exactly what you described in your question.

tobi_p

Posted 2012-02-28T05:14:57.577

Reputation: 21

This only somewhat works.. mkdir AaBbCcDd cd ABCD (tab) => cd ./ ie. Autocomplete fails – Rohit Manokaran – 2012-04-12T19:32:54.617