How can I make zsh tab completion fix capitalization errors for directories and files?

17,195

TL;DR: This is possible if you put these lines in your zsh config file, usually ~/.zshrc:

autoload -Uz compinit && compinit
zstyle ':completion:*' matcher-list 'm:{a-z}={A-Za-z}'

A little bit more information:

This is possible when using the zsh completion system (started by autoload -Uz compinit && compinit) and is controlled by a zstyle:

zstyle ':completion:*' matcher-list 'm:{a-z}={A-Za-z}'

This tells zsh that small letters will match small and capital letters. (i.e. capital letters match only capital letters.)

If you want that capital letters also match small letters use instead:

zstyle ':completion:*' matcher-list 'm:{a-zA-Z}={A-Za-z}'

If you want case-insensitive matching only if there are no case-sensitive matches add '', e.g.

zstyle ':completion:*' matcher-list '' 'm:{a-zA-Z}={A-Za-z}'

See also the description of matcher-list in man zshcompsys.

Share:
17,195

Related videos on Youtube

Aaron
Author by

Aaron

Updated on September 18, 2022

Comments

  • Aaron
    Aaron over 1 year

    I switched to zsh completely on a previous arch install and I LOVED it mainly because arch is one of the distros that capitalizes your Documents, Downloads, Music and such directories in your ~ directory. Since I wasn't used to that and I use tab completion for almost everything in the terminal zsh was like heaven for me compared to bash.... I swear this feature used to be active automatically because I don't remember having to tweak anything to make it work that way. I probably figured it out on accident. xD But I would greatly like to have this feature restored on my new pc using zsh as my main shell, and if anyone knows how to do this I would really appreciate a reply. I tried activating every option in the completion configuration and that didn't seem to do the trick... so that brought me here.

    PS: I used to be a lot more up to date with my Linux know-how, and my knowledge has grown stale... I'm trying to remedy that... (without the use of the shift key, as much as possible... haha)

    • mpy
      mpy almost 8 years
      Welcome to SU! To improve your style a comment: Your question is rather difficult to read, because there is a lot of unnecessary information. Please edit you question and try to concentrate on the relevant infos. Keep it short and simple ;).
  • mpy
    mpy almost 8 years
    @Aaron: Glad to hear. Please consider to accept my answer then as explained in the SU tour.
  • inherithandle
    inherithandle over 5 years
    So where do I put zstyle?
  • mpy
    mpy over 5 years
    @inherithandle: It's a normal shell (builtin) command, you can execute it on the command line. If you wan't that behavior permanent, put it into your ~/.zshrc.
  • jdhao
    jdhao over 4 years
    I have added the above setting to my .zshrc and sourced it, but it does not seem to work. I have also tried to use the command in command line to no avail. Any ideas on how to make it work?
  • mpy
    mpy over 4 years
    @jdhao: Maybe you haven't initialized the completion system, use some other 3rd party settings (d'oh-my-zsh), etc... so, this is too complex to debug in the comments. Try to construct a minimal(!) case of your config files and write a separate question; something like "Why have my zstyle settings no effect?"
  • jdhao
    jdhao over 4 years
    @mpy Thanks for your kind reply. I have found that we need to add autoload -Uz compinit && compinit after the zstyle command to make the setting take effect. Maybe it is worthwhile to mention that in the answer for zsh novices like me...
  • mpy
    mpy over 4 years
    @jdhao: Yes, good point. I added this to the introductory sentence.
  • jitbit
    jitbit over 2 years
    Please rewrite the answer so one doesn't have to scan all the comments to figure out what one needs to do. Like: (1) create a file (2) add "A" to the file (3) add "B" to the file This is superuser, not Serverfault or Stackoverflow ;)
  • mpy
    mpy over 2 years
    @jitbit: I added a "summary" with a snippet for easy copy&paste. I hope this will help to get this working faster.
  • jitbit
    jitbit over 2 years
    @mpy thank you sir!