zoxide
zoxide copied to clipboard
zsh cd completions duplicate with group-name enabled
I stripped down my completions setup in zshrc to be just:
autoload -Uz compinit && compinit -C
eval "$(zoxide init -zsh)"
Now, I add a format and group-name setting and the strange behavior starts.
When using cd tab completion, I get expected suggestions. When using z, I get a duplicate set of suggestions.

For reference, this is how to reproduce:
$ cd $(mktemp -d)
$ touch .zshrc
$ ZDOTDIR=${PWD} HOME=${PWD} exec zsh
$ autoload -Uz compinit && compinit -C
$ eval "$(zoxide init zsh)"
$ zstyle ':completion:*' format '%F{yellow}-- %d --%f'
$ zstyle ':completion:*' group-name ''
$ mkdir test{1,2}
$ z test<TAB>
-- file --
test1/ test2/
-- file --
test1/ test2/
@grimm26, I noticed it works as expected if you do eval "$(zoxide init zsh)" before autoload -Uz compinit && compinit -C, which is the opposite of what the zoxide documentation recommends:
For completions to work, the above line must be added after
compinitis called. You may have to rebuild your cache by runningrm ~/.zcompdump*; compinit.
@ericbn You are correct! I have no idea how/why but it works. Maybe just a documentation change is needed?
I noticed it works as expected if you do eval "$(zoxide init zsh)" before autoload -Uz compinit && compinit -C
Not easy to do if you use prezto which does the autoload in its modules. ohmyzsh may have the same issue too.
FWIW, zi doesn't have the duplicate behavior.
I'm also experiencing this issue, but calling eval "$(zoxide init zsh)" before autoload -Uz compinit && compinit -C seems to completely break completions for me.
I found a solution haha. I'm using zi so zoxide is run before compinit, but I still got the duplicate. Turns out the __z commands default to a certain completion if no compdef is provided. To override this, add this: compdef __zoxide_z=vim; compdef __zoxide_zi=vim;.
I found a solution haha. I'm using zi so zoxide is run before compinit, but I still got the duplicate. Turns out the __z commands default to a certain completion if no compdef is provided. To override this, add this: compdef __zoxide_z=vim; compdef __zoxide_zi=vim;.
That's not a solution, this makes it so the completions for zoxide will be the completions of vim.
I'm also experiencing this issue, but calling
eval "$(zoxide init zsh)"beforeautoload -Uz compinit && compinit -Cseems to completely break completions for me.
Same
For anyone looking for a workaround, I got things working by replacing _files -/ in __zoxide_z_complete() with _path_files -/. Despite it being recommended against, this is also incidentally what the builtin _cd completion function for cd calls.
Here's the full completions snippet with the fix:
# Completions.
if [[ -o zle ]]; then
function __zoxide_z_complete() {
# Only show completions when the cursor is at the end of the line.
# shellcheck disable=SC2154
[[ "${#words[@]}" -eq "${CURRENT}" ]] || return 0
if [[ "${#words[@]}" -eq 2 ]]; then
# call _path_files directly to avoid duplication issue #491
_path_files -/
elif [[ "${words[-1]}" == '' ]] && [[ "${words[-2]}" != "${__zoxide_z_prefix}"?* ]]; then
\builtin local result
# shellcheck disable=SC2086,SC2312
if result="$(\command zoxide query --exclude "$(__zoxide_pwd)" --interactive -- ${words[2,-1]})"; then
result="${__zoxide_z_prefix}${result}"
# shellcheck disable=SC2296
compadd -Q "${(q-)result}"
fi
\builtin printf '\e[5n'
fi
return 0
}
\builtin bindkey '\e[0n' 'reset-prompt'
[[ "${+functions[compdef]}" -ne 0 ]] && \compdef __zoxide_z_complete __zoxide_z
fi
this bug is quite annoying, I'm surprised more people have not been running into it
This solved it for me #787
This solved it for me #787
~Seems to be the fix that I have posted above. Glad someone picked it up and submitted a PR.~
EDIT: scratch that. The PR uses the builtin cd auto completion.
I do notice that using _cd vs _path_file gives slightly different result in that it is titled with local when using _cd. :shrug:
I do notice that using
_cdvs_path_filegives slightly different result in that it is titled with local when using_cd. :shrug:
Yes, nevermind my previous comment. Not sure where my head was. I remember considering just applying the builtin autocompletion (like the PR does) and then discarding it for _path_file. I don't quite recall my reasoning atm, will look into it later.
I met this problem too. As @urob mentioned you shouldn't change the autocomplete behaviour, but changing the _files -/ line helps.
Since the upper referenced 787 pull request replaces the _files -/ line with _cd -/ I did that too.
For a simple sed solution I put this code in my ~/.zshrc: eval "$(zoxide init --cmd cd zsh | sed 's/_files/_cd/g')".