feat(completions): unify cd/z completions across CWD, CDPATH, and zoxide

Previous to this commit, tab completion and auto-suggestions for cd/z
were inconsistent — only one or two of: CWD, CDPATH, and zoxide frecency
results would work at a time, with different sourced matches shown in
tab completion, shown in auto-suggest, and execution after pressing
<Enter>.

- Add _zoxide_z_complete in functions/zoxide.fish that merges all three
  sources into a single completion list (CWD via __fish_complete_cd,
  CDPATH via __fish_complete_directories, zoxide via query -l capped at
25)
- Wire the new completer to both z and cd via complete directives in
  conf.d/zoxide.fish, replacing the previous incomplete approach
- Add completions/zoxide.fish for full tab completion of the zoxide CLI
  itself (add, query, remove, import, init subcommands)
- Update README to document the unified completion behavior and fix
  structural issues in Personalization/Attribution/Dependencies sections
This commit is contained in:
2026-05-11 23:26:33 -04:00
parent f93f9844dc
commit 2e3230974c
4 changed files with 158 additions and 100 deletions
+57 -95
View File
@@ -1,106 +1,68 @@
# =============================================================================
#
#
# Utility functions for zoxide.
#
# Adapted from icezyclon/zoxide.fish (MIT)
# Heavily customized for Fish 4.x compatibility and performance
if not type -q zoxide
return
end
if status is-interactive
# pwd based on the value of _ZO_RESOLVE_SYMLINKS.
function __zoxide_pwd
builtin pwd -L
end
if type -q zoxide
# A copy of fish's internal cd function. This makes it possible to use
# `alias cd=z` without causing an infinite loop.
if ! builtin functions --query __zoxide_cd_internal
string replace --regex -- '^function cd\s' 'function __zoxide_cd_internal ' <$__fish_data_dir/functions/cd.fish | source
end
# -------------
# 'zoxide init fish' is very different for different versions of zoxide
# to guarantee the same behavior we define these functions ourself,
# especially because the apt package is so old
# most of these functions were taken from https://github.com/ajeetdsouza/zoxide
# from version 0.8.1
# cd + custom logic based on the value of _ZO_ECHO.
function __zoxide_cd
if set -q __zoxide_loop
builtin echo "zoxide: infinite loop detected"
builtin echo "Avoid aliasing `cd` to `z` directly, use `zoxide init --cmd=cd fish` instead"
return 1
end
__zoxide_loop=1 __zoxide_cd_internal $argv
end
if ! builtin functions -q _zoxide_cd
if builtin functions -q cd
builtin functions -c cd _zoxide_cd
else
alias _zoxide_cd='builtin cd'
end
end
# =============================================================================
#
# Hook configuration for zoxide.
#
function _zoxide_hook --on-variable PWD
test -z "$fish_private_mode"
and command zoxide add -- (builtin pwd -L)
end
# Initialize hook to add new entries to the database.
function __zoxide_hook --on-variable PWD
test -z "$fish_private_mode"
and command zoxide add -- (__zoxide_pwd)
end
function z
set argc (count $argv)
if test $argc -eq 0
_zoxide_cd $HOME
else if test "$argv" = -
_zoxide_cd -
else if test -d $argv[-1]
_zoxide_cd $argv[-1]
else
set -l result (command zoxide query $argv)
and _zoxide_cd $result
end
end
# =============================================================================
#
# When using zoxide with --no-cmd, alias these internal functions as desired.
#
function zi
set -l result (command zoxide query -i -- $argv)
and _zoxide_cd $result
end
# Jump to a directory using only keywords.
function __zoxide_z
set -l argc (builtin count $argv)
if test $argc -eq 0
__zoxide_cd $HOME
else if test "$argv" = -
__zoxide_cd -
else if test $argc -eq 1 -a -d $argv[1]
__zoxide_cd $argv[1]
else if test $argc -eq 2 -a $argv[1] = --
__zoxide_cd -- $argv[2]
# -------------
alias cd=z
# use custom completion
complete -c z -f # disable files by default
complete -c z -x -a '(_zoxide_z_complete)'
else
set -l result (command zoxide query --exclude (__zoxide_pwd) -- $argv)
and __zoxide_cd $result
echo "[plugin: zoxide] Command 'zoxide' cannot be found. Not installed or not in path"
end
end
function _zoxide_uninstall --on-event zoxide_uninstall
if alias | grep "alias cd z" >/dev/null
functions -e cd
end
if builtin functions -q _zoxide_cd && not functions -q cd
# restore old cd
builtin functions -c _zoxide_cd cd
end
end
# Completions.
function __zoxide_z_complete
set -l tokens (builtin commandline --current-process --tokenize)
set -l curr_tokens (builtin commandline --cut-at-cursor --current-process --tokenize)
if test (builtin count $tokens) -le 2 -a (builtin count $curr_tokens) -eq 1
# If there are < 2 arguments, use `cd` completions.
complete --do-complete "'' "(builtin commandline --cut-at-cursor --current-token) | string match --regex -- '.*/$'
else if test (builtin count $tokens) -eq (builtin count $curr_tokens)
# If the last argument is empty, use interactive selection.
set -l query $tokens[2..-1]
set -l result (command zoxide query --exclude (__zoxide_pwd) --interactive -- $query)
and __zoxide_cd $result
and builtin commandline --function cancel-commandline repaint
end
end
complete --command __zoxide_z --no-files --arguments '(__zoxide_z_complete)'
# Jump to a directory using interactive search.
function __zoxide_zi
set -l result (command zoxide query --interactive -- $argv)
and __zoxide_cd $result
end
# =============================================================================
#
# Commands for zoxide. Disable these using --no-cmd.
#
abbr --erase z &>/dev/null
alias z=__zoxide_z
abbr --erase zi &>/dev/null
alias zi=__zoxide_zi
abbr --erase cdi &>/dev/null
alias cdi=__zoxide_zi
# =============================================================================
# Initialize zoxide:
zoxide init --cmd=cd fish | source