This happens because Zsh sees zsh-test/
as an unambiguous prefix in the first case, whereas in the second case, there is no unambiguous prefix. (Yes, there's an unambiguous suffix, but Zsh doesn't do anything with that, unfortunately.) And by default, if Zsh sees an unambiguous prefix, then the first press of Tab will simply insert that prefix, possibly deleting characters you've already typed.
You can test this by entering the following into the shell:
autoload -Uz compinit && compinit
zstyle ':completion:*' matcher-list 'm:{[:lower:]}={[:upper:]} r:|?=**'
bindkey '^I' complete-word
zle -C complete-word complete-word complete-word
complete-word() {
_main_complete;
compadd -x "unambiguous prefix: '${compstate[unambiguous]}'";
compstate[list]='list force';
}
Then, try your two cases again. For the first case, you will see unambiguous prefix: 'zsh-test/'
. For the second case, you will see unambiguous prefix: ''
There is, unfortunately, no way to configure Zsh to get the behavior you want. However, if you are willing to go a bit deeper down the rabbit hole, then the behavior you desire can be achieved by adding the following script to your .zshrc
file:
zmodload -i zsh/complist
autoload -Uz compinit && compinit
zstyle ':completion:*' matcher-list 'm:{[:lower:]}={[:upper:]} r:|?=**'
bindkey '^I' complete-word
zle -C complete-word menu-select complete-word
complete-word() {
_main_complete;
compstate[list]='list';
local word=$PREFIX$SUFFIX
(( compstate[unambiguous_cursor] <= ${#word} )) && compstate[insert]='menu';
}
compstate[list]='list'
makes sure that completions are automatically listed when there's more than one match available.
- The last line checks whether the default behavior would delete any part of the current word that the cursor is on and, if so, instead inserts the first item from the list.
- The
menu-select
(provided by zsh/complist
) in zle -C complete-word menu-select complete-word
then lets you use your choice of Tab or the arrow keys to select a different item from the list.
Tuning Zsh's completion system this way can get fairly complex rather quickly and there are many parts of it that are not all that well-documented. I highly encourage you to check out my zsh-autocomplete
plugin, which customizes the Zsh completion system to do out-of-the-box what most people would expect it to do. 🙂