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

Support the -- command-line argument #776

Open
igor-akhmetov opened this issue Mar 31, 2024 · 4 comments
Open

Support the -- command-line argument #776

igor-akhmetov opened this issue Mar 31, 2024 · 4 comments

Comments

@igor-akhmetov
Copy link

A very minor request, but it'd be nice if zoxide supported the -- command-line argument similar to how cd does it. Right now zoxide treats it as a path and reports that "no match found".

A use case scenario with bash:

  1. Use zoxide init bash --cmd cd to replace the built-in cd.
  2. Set shopt -s autocd to make bash automatically cd when a command name is a directory.
  3. Enter a command name that is a directory. Bash uses cd -- <path>, the cd alias expands to a zoxide command, and zoxide fails because it doesn't recognize --.
@pallaswept
Copy link

+1. Just came here to report that zoxide isn't working with autocd.

@pallaswept
Copy link

pallaswept commented Apr 19, 2024

Here's a workaround for anyone else who is trying to get this to behave - we will override the internal zoxide function and catch the leading -- before it is passed along with another -- making an invalid set of arguments:

function __zoxide_z() {
    # shellcheck disable=SC2199
    if [[ $# -eq 0 ]]; then
        __zoxide_cd ~
    elif [[ $# -eq 1 && $1 == '-' ]]; then
        __zoxide_cd "${OLDPWD}"
    elif [[ $# -eq 1 && -d $1 ]]; then
        __zoxide_cd "$1"
    elif [[ ${@: -1} == "${__zoxide_z_prefix}"?* ]]; then
        # shellcheck disable=SC2124
        \builtin local result="${@: -1}"
        __zoxide_cd "${result:${#__zoxide_z_prefix}}"
    else
        \builtin local result
        # Allow for leading '--' in args from autocd etc. See issue #776
        while [[ $1 == '--' ]]; do \builtin shift; done
        # shellcheck disable=SC2312
        result="$(\command zoxide query --exclude "$(__zoxide_pwd)" -- "$@")" &&
            __zoxide_cd "${result}"
    fiinit
}

This is just the normal __zoxide_z function as generated by zoxide init, with one line of code (and one comment) to strip the leading -- from the arguments, since one is hardcoded and more is broken. This one is for bash, so you might need to adjust it to work with your shell of choice. I'll be happy to help if you're stuck :)

I would submit this as a PR, but I'm brand new around here and have no idea if this is the 'right way' to do it for this project.... But this works just fine, for now :)

@anasouardini
Copy link

anasouardini commented Apr 20, 2024

Not just the autocd but sometimes other tools change directory for you and they do it in a way that's not covered by zoxide.
A good solution is to watch when current directory PWD changes which is a hook that's provided by most shells, if not, there must be a hook that runs whenever the prompt is changed and then you can run $(pwd) to get the current dir and compare it.

@pallaswept
Copy link

they do it in a way that's not covered by zoxide.

If it isn't the prepended -- that is reported by OP and I, you might want to log a separate case, but, specify it, and I'll see what I can do :)

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

3 participants