support autocd option

In bash, when autocd option is set and user enters a directory name as a
command, it results in a very specific call to cd:

cd -- [directory name]

zoxide's directory changing function passes all arguments to __zoxide_z as is,
including the "--" first argument. By detecting this and skipping the first
argument changing directory works with autocd set.
This commit is contained in:
Peter Solodov 2024-02-14 18:46:39 -05:00
parent cbb8e77d60
commit b50ee1d9ea
1 changed files with 7 additions and 1 deletions

View File

@ -102,7 +102,13 @@ function __zoxide_zi() {
\builtin unalias {{cmd}} &>/dev/null || \builtin true
function {{cmd}}() {
__zoxide_z "$@"
if [[ $# -eq 2 && "$1" == "--" && -d $2 ]]; then
# This is how cd is called when bash's autocd option is set. To get the
# directory-changing behavior first argument must be skipped.
__zoxide_z "${@:2}"
else
__zoxide_z "$@"
fi
}
\builtin unalias {{cmd}}i &>/dev/null || \builtin true