junegunn / fzf

:cherry_blossom: A command-line fuzzy finder
https://junegunn.github.io/fzf/
MIT License
65.02k stars 2.39k forks source link

How to turn on fzf help completion at zsh? #3349

Open Shuraken007 opened 1 year ago

Shuraken007 commented 1 year ago

Hello, greped a lot in Issues and manual, but can't find how to enable zsh completion for fzf help. I mean tab complete for fzf options like --preview, --multi, e.t.c.

shell/completion.bash has function _fzf_opts_completion with opts image

I havn't find something like this for shell/completion.zsh image probably you added some scripts to build help auto?

There are some examples bat: https://github.com/sharkdp/bat/blob/master/assets/completions/bat.zsh.in exa: https://github.com/ogham/exa/blob/master/completions/zsh/_exa ripgrep: https://github.com/BurntSushi/ripgrep/blob/master/complete/_rg

LangLangBart commented 1 year ago

No, fzf does not have that. Someone has to write a zsh completion function for it.

Completion functions for commands are stored in files with names beginning with an underscore , and these files should be placed in a directory listed in the $fpath variable. Source: HOWTO Guide zsh-users/zsh-completions

Some tools have it, for example gh or bat. If you have Homebrew installed, you can see such files or checkout the zsh-users/zsh-completions github repo.

ls -1 $(brew --prefix)/share/zsh/site-functions
# _bat
# _brew
# _brew_services
# _cargo
# _delta
# _exa
# _fd
# _gh
# _git
# _hx
# _ninja
# _parallel
# _revolver
# _rg
# _yt-dlp
# _zoxide

Alternatively, you can run compdef _gnu_generic fzf which will search the fzf help page and list the flags found, but as the name implies, it is generic and will not work on specific options passed to flags.

# you may have to run those commands as well
autoload -Uz compinit
compinit

# run
# add the line to your '.zshrc' file if it is useful for your case
compdef _gnu_generic fzf

demo

related

LangLangBart commented 1 year ago

git clone --depth 1 https://github.com/lmburns/dotfiles.git lmburns_zsh_completion

# .zshrc
export FPATH="$FPATH:$HOME/lmburns_zsh_completion/.config/zsh/completions"
cd $HOME/my_completion_folder
curl --remote-name https://raw.githubusercontent.com/lmburns/dotfiles/master/.config/zsh/completions/_fzf

# .zshrc
export FPATH="$FPATH:$HOME/my_completion_folder"
Shuraken007 commented 1 year ago

@LangLangBart - how have you founded this example? Used git search engine for files with name _fzf?

LangLangBart commented 1 year ago

_@LangLangBart - how have you founded this example? Used git search engine for files with name fzf?

GitHub WebUI

I posted the search link in my previous comment. See docs.github.com/understanding-github-code-search-syntax for more details.


Command line

# uses 'gh', 'fzf' and 'bat'
gh search code $'#compdef fzf' \
    --filename '_fzf' \
    --json repository,path \
    --jq '.[] | [.repository.nameWithOwner, .path] | @tsv' |
    fzf --delimiter '\t' --with-nth 1 \
        --preview 'gh api --cache 10m repos/{1}/contents/{2} \
            --jq .content | base64 --decode | bat --language zsh --number' \
        --preview-window 'nohidden:right:wrap:75%' \
        --bind 'ctrl-b:execute-silent:gh browse --repo {1} {2}'
Shuraken007 commented 1 year ago

@LangLangBart whoa, amazing tricks with that utils. Thanks for this cool staff. You are on the crest of a wave, cli added code search 2 weeks ago. https://github.com/cli/cli/releases/tag/v2.31.0 had to manually install release from github

I had to use a bit other params for bat bat --language zsh --style=numbers --color=always

wanna check your dotfiles to get some usefull tricks, but looks like they are private.

LangLangBart commented 1 year ago

I had to use a bit other params for bat bat --language zsh --style=numbers --color=always

I missed the --color=always, it was only set in my .zshrc, and --number is just an alias for --style=numbers

wanna check your dotfiles to get some usefull tricks, but looks like they are private.

Yes. I use zsh4humans as my setup. Checkout the creator's dotfiles - a true zsh wizard.


PS: The code snippet from my previous comment was transformed into an extension - gh find-code, utilizing the newest features of fzf.

chrisgrieser commented 8 months ago

I second that having completions would be nice. Even incomplete completions would be better than none at all. Maybe @lmburns would be willing to PR their completions?

@LangLangBart Thanks for the various tricks you shared in this thread. compdef _gnu_generic and the github code search really useful.

iloveitaly commented 3 months ago

https://github.com/RobSis/zsh-completion-generator is another option here for generating completions for tools which don't provide them.