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

Allow non-consecutive characters in query #388

Open
knutwannheden opened this issue May 2, 2022 · 4 comments
Open

Allow non-consecutive characters in query #388

knutwannheden opened this issue May 2, 2022 · 4 comments
Labels
feature Request for a new feature.

Comments

@knutwannheden
Copy link

Maybe I am missing something, but if I in my history have a directory like framework I would like to be able to type z fwk to change to that directory. Since fzf generally allows for this kind of thing, I was expecting to be able to do that with zoxide, but I couldn't find any way. If I am not missing anything, then please consider this as an enhancement request for a corresponding option.

@ajeetdsouza
Copy link
Owner

I'm don't think fzf's algorithm is a great fit for zoxide. The use case is entirely different:

fzf is an interactive program:

  • you can type in more characters to refine your query
  • it can make mistakes, you can scroll around if the best match isn't on top

zoxide, on the other hand:

  • deals with very few characters (most of my queries are 2-3 characters). If you allow non-consecutive characters, that dramatically increases the number of possible matches.
  • has to come up with the correct answer in one try, there's no scope of refining the query or scrolling to the second best match

@knutwannheden
Copy link
Author

Thanks for your feedback. I don't quite agree with your concerns, so let me elaborate.

The user will know (or quickly learn) what characters he needs to type to get a unique match, so I don't think the number of matches would increase. And the idea was that this matching algorithm would be an alternative non-default strategy, so users could continue using zoxide like they are used to.

If I expand my previous example to include the directories framework-a, framework-b, client-a, and client-b (this is fictitious but large organizations with many Git repos often have such strict naming conventions), I would be able to type fa (or fwa or similar) which I think would be really nice as an alternative.

@ajeetdsouza ajeetdsouza added the feature Request for a new feature. label May 9, 2022
@evanrelf
Copy link

evanrelf commented Sep 6, 2022

You could allow non-consecutive characters in queries when using zi, since it will pull up fzf to refine your search.

@ProspectPyxis
Copy link

ProspectPyxis commented Dec 13, 2022

For anyone looking to do this right now, I've managed to hack the shell functions to use fzf on top of zoxide query. Put one of the following after the line that adds zoxide to your shell:

Bash/Zsh
function __zoxide_z() {
    if [[ "$#" -eq 0 ]]; then
        __zoxide_cd ~
    elif [[ "$#" -eq 1 ]] && { [[ -d "$1" ]] || [[ "$1" = '-' ]] || [[ "$1" =~ ^[- ][0-9]$ ]]; }; then
        __zoxide_cd "$1"
    elif [[ "$@[-1]" == "${__zoxide_z_prefix}"* ]]; then
        \builtin local result="${@[-1]}"
        __zoxide_cd "${result:${#__zoxide_z_prefix}}"
    else
        \builtin local IFS=' '
        \builtin local result
        result="$(\command zoxide query --list --exclude "$(__zoxide_pwd)" | \command fzf --no-sort --filter "$*" | \command head -n 1)" &&
            __zoxide_cd "${result}"
    fi
}
Fish
function __zoxide_z
    set -l argc (count $argv)
    set -l completion_regex '^'(string escape --style=regex $__zoxide_z_prefix)'(.*)$'

    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 set -l result (string match --groups-only --regex $completion_regex $argv[-1])
        __zoxide_cd $result
    else
        set -l result (command zoxide query --list --exclude (__zoxide_pwd) | command fzf --no-sort --filter (string join " " $argv) | command head -n 1)
        and __zoxide_cd $result
    end
end

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

No branches or pull requests

4 participants