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

Max picture Max · Jun 15, 2014 · Viewed 20.1k times · Source

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!

Answer

Adaephon picture Adaephon · Jun 16, 2014

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)