ajeetdsouza / zoxide

A smarter cd command. Supports all major shells.
MIT License
21.9k stars 535 forks source link

Allow non-consecutive characters in query #388

Open knutwannheden opened 2 years ago

knutwannheden commented 2 years ago

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 commented 2 years ago

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:

zoxide, on the other hand:

knutwannheden commented 2 years ago

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.

evanrelf commented 2 years ago

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

ProspectPyxis commented 1 year ago

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 ```bash 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 ```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 ```