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

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

Open
Shuraken007 opened this issue Jul 1, 2023 · 8 comments
Open

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

Shuraken007 opened this issue Jul 1, 2023 · 8 comments

Comments

@Shuraken007
Copy link

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
Copy link
Contributor

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
Copy link
Contributor

LangLangBart commented Jul 5, 2023


  • either clone the entire repo and add the path to the completions directory to your $FPATH or
git clone --depth 1 https://github.com/lmburns/dotfiles.git lmburns_zsh_completion

# .zshrc
export FPATH="$FPATH:$HOME/lmburns_zsh_completion/.config/zsh/completions"
  • download this individual file, save it in some folder and add this folder to your $FPATH
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
Copy link
Author

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

@LangLangBart
Copy link
Contributor

LangLangBart commented Jul 5, 2023

_@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
Copy link
Author

Shuraken007 commented Jul 6, 2023

@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
Copy link
Contributor

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
Copy link

chrisgrieser commented Feb 1, 2024

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
Copy link

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

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

4 participants