Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

possible configuration issue when using PROMPT_COMMAND as an array #987

Open
aarondill opened this issue Feb 12, 2025 · 1 comment
Open

Comments

@aarondill
Copy link
Contributor

having recently updated zoxide, i receive a warning about a possible configuration issue.
Having seen the other issues, I assume this is due to the expansion of "$PROMPT_COMMAND" not including the zoxide hook.
In my case, this is not a misconfiguration. As I intentionally override PROMPT_COMMAND to be an array.
My initialization code (note that I've modularized my .bashrc:

# if `z` is installed, initiate it and set `cd` to `z`
if ! zoxide -V &>/dev/null; then return 0; fi

# HACK: Zoxide overwrites the PROMPT_COMMAND, so we must move it out of it's view, and restore it afterwards
OLD_PROMPT_COMMAND=("${PROMPT_COMMAND[@]}")
PROMPT_COMMAND="" # only overwrites PROMPT_COMMAND[0], but it doesn't matter

export _ZO_ECHO=0 # Fixes warning if set -u
eval "$(zoxide init bash)"

PROMPT_COMMAND=("$PROMPT_COMMAND" "${OLD_PROMPT_COMMAND[@]}") # prepend, since it's trying to prepend anyways
unset OLD_PROMPT_COMMAND

# Reuse cd completion if present (overwritten already), else default to directories
complete=$(complete -p cd 2>/dev/null || printf '%s ' complete -d cd)
# remove ' cd' and replace with ' z'. Lack of quotes is intentional!
if [ -n "$complete" ]; then
  eval "${complete% cd} z"
fi
alias cd='z'

I'd like to request that the _ZO_DOCTOR prompt checks that any element of ${PROMPT_COMMAND[@]} is the __zoxide_hook
Note:

> printf '%s\n' "${PROMPT_COMMAND[@]}"
__EXIT=$?
__zoxide_hook;
starship_precmd
history -a
(exit $__EXIT)
@aarondill
Copy link
Contributor Author

Please note that in bash, arrays and scalars are (mostly) interchangeable.
If an array is expanded normally, it evaluates to the first element; if a scalar is treated as an array, it is an array of one element.

> a=(1 2 3)
> echo "${a[@]}"
1 2 3
> echo "$a"
1
> b=hello
> echo "$b"
hello
> echo "${b[@]}"
hello

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant