zoxide icon indicating copy to clipboard operation
zoxide copied to clipboard

Allow non-consecutive characters in query

Open knutwannheden opened this issue 3 years ago • 4 comments

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.

knutwannheden avatar May 02 '22 06:05 knutwannheden

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

ajeetdsouza avatar May 07 '22 05:05 ajeetdsouza

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.

knutwannheden avatar May 07 '22 05:05 knutwannheden

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

evanrelf avatar Sep 06 '22 23:09 evanrelf

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

ProspectPyxis avatar Dec 13 '22 06:12 ProspectPyxis