fix: use runtime fallback for zoxide cd function#591
Open
ki11e6 wants to merge 3 commits intobasecamp:masterfrom
Open
fix: use runtime fallback for zoxide cd function#591ki11e6 wants to merge 3 commits intobasecamp:masterfrom
ki11e6 wants to merge 3 commits intobasecamp:masterfrom
Conversation
Only alias cd to z if __zoxide_z function was properly initialized. This prevents broken alias errors in non-interactive shells (e.g., Claude Code) where zoxide init may not fully set up the z function.
65e8247 to
d5c5606
Compare
Replace source-time alias check with runtime function fallback. This approach checks if __zoxide_z is available at execution time, allowing graceful fallback to builtin cd in non-interactive shells (e.g., Claude Code) while still supporting zoxide in interactive shells. Ref: anthropics/claude-code#2407
Kasui92
reviewed
Jan 9, 2026
Contributor
Kasui92
left a comment
There was a problem hiding this comment.
I think it's more correct to separate init from aliases.
In this #562 I'm introducing the same configuration that was used on Omarchy, which solves this problem.
if command -v zoxide &> /dev/null; then
alias cd="zd"
zd() {
if [ $# -eq 0 ]; then
builtin cd ~ && return
elif [ -d "$1" ]; then
builtin cd "$1"
else
z "$@" && printf "\U000F17A9 " && pwd || echo "Error: Directory not found"
fi
}
fi
Author
Improved approach inspired by Omarchy (thanks @Kasui92!): - Use builtin cd directly for no args (home) and existing directories - Only use zoxide for fuzzy matching - Fallback to builtin cd if zoxide unavailable (non-interactive shells) More efficient - skips function check for common cases.
e5a938a to
e550192
Compare
Kasui92
reviewed
Jan 10, 2026
Contributor
Kasui92
left a comment
There was a problem hiding this comment.
I think so, that might be enough, even just to have a similar alignment with what's being done on Omarchy.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
alias cd='z'fromdefaults/bash/aliasescd()function indefaults/bash/initwith runtime fallback tobuiltin cdProblem
The
cdcommand fails withbash: command not found: __zoxide_zin non-interactive shell environments (e.g., Claude Code CLI, scripts, CI/CD pipelines).Root cause: Zoxide's
__zoxide_zfunction is not available in non-interactive shells becauseeval "$(zoxide init bash)"doesn't properly define functions in these contexts. See: anthropics/claude-code#2407Previous Fix Attempt (didn't fully work)
Why it failed: This checks only once when the file is sourced. In some environments, the shell state can become inconsistent (functions defined but later unavailable), or tools like
misecan override thecdfunction after our alias is set.New Fix (runtime fallback)
Why this works:
__zoxide_zis availablebuiltin cdwhen not (non-interactive shells)Test Results
__zoxide_zbuiltin cdbuiltin cdTest plan
cduses zoxide in interactive shellscdfalls back to builtin in non-interactive shells__zoxide_zerrors in any environment..,...aliases still work in interactive shells