Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Have zsh return case-insensitive auto-complete matches, but prefer exact matches

Tags:

zsh

oh-my-zsh

I am using zsh with oh-my-zsh's rc file and there is some behavior I find particularly annoying. By default, oh-my-zsh is configured to return case-insensitive matches when auto-completing. This behavior is sometimes good, but other times it really sucks. Is there a way I can configure zsh to only use case-insenstive matching when there are no case-sensitive matches?

For instance, this case would use case-sensitive matching:

> ls LICENSE.txt    lib/ > emacs l <-- should autocomplete to lib/ 

In this case, case-insensitive auto-completion would happen:

> ls README    lib/ > emacs r <-- should autocomplete to README 

Thanks!

like image 544
Max Avatar asked Jun 15 '14 04:06

Max


2 Answers

Create a file ~/.oh-my-zsh/custom/better-completion.zsh (assuming you are using default paths for oh-my-zsh) with the following lines

zstyle ':completion:*' matcher-list '' 'm:{a-zA-Z}={A-Za-z}' 'r:|[._-]=* r:|=*' 'l:|=* r:|=*' 

Explanation:

Rules for matches in zsh completion in general are defined in the matcher-list style. For oh-my-zsh this is defined in ~/.oh-my-zsh/lib/completion.zsh (once for case-sensitive and once for case-insensitive). You could change it there but it would probably be gone if you updated your oh-my-zsh. ~/.oh-my-zsh/custom is specifially intended for customization and files with extension .zsh are loaded from there by .oh-my-zsh/oh-my-zsh.sh at the end of the configuration.

The default (case-insensitive) settings for matcher-list in oh-my-zsh are:

zstyle ':completion:*' matcher-list 'm:{a-zA-Z}={A-Za-z}' 'r:|[._-]=* r:|=*' 'l:|=* r:|=*' 

The first of which tells to handle upper and lower case interchangeable. As it is the first rule, it will be invariably used for every match.

The only change needed is to prepend '' for simple completion (it is even the first example in zshcompsys(1) for matcher-list)

zstyle ':completion:*' matcher-list '' 'm:{a-zA-Z}={A-Za-z}' 'r:|[._-]=* r:|=*' 'l:|=* r:|=*' 

This tries first to complete the current word exactly as its written, before trying case-insensitive or other matches.

To be complete:

  • The second (original) rule allows for partial completion before ., _ or -, e.g. f.b -> foo.bar.
  • The third rule allows for completing on the left side of the written text, e.g. bar -> foobar)
like image 96
Adaephon Avatar answered Sep 28 '22 15:09

Adaephon


Just uncomment the following line in ~/.zshrc:

# Uncomment the following line to use case-sensitive completion. # CASE_SENSITIVE="true" 

It worked for me

like image 35
Jesus Garcia Avatar answered Sep 28 '22 16:09

Jesus Garcia