Skip to content
Myr

zsh: support sourcing zsh completion dynamically

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Previously, you needed to save the completion script to a file and then source it. Now, you can dynamically source completions in zsh by running $ source <(rg --generate complete-zsh) Before this commit, you would get an error after step 1. After this commit, it should work as expected. We also improve the FAQ item for zsh completions. Fixes #2956

authored by Max Coplan · committed by GitHub · parent 79cbe89 · Browse files · 2 files+29 added−4 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+29 added−4 removed

  • other:+29 added−4 removed

2 files+29 added−4 removed

modified:crates/core/flags/complete/rg.zsh+9 added-1 removed
432 unmodified lines
433 _wanted types expl 'file type' compadd "$@" - ${(@)_types%%:*}434 fi435}436437# Don't run the completion function when being sourced by itself.438#439# See https://github.com/BurntSushi/ripgrep/issues/2956440# See https://github.com/BurntSushi/ripgrep/pull/2957441if [[ $funcstack[1] == _rg ]] || (( ! $+functions[compdef] )); then437_rg "$@"442 _rg "$@"443else444 compdef _rg rg445fi446447################################################################################448# ZSH COMPLETION REFERENCE449#
196 unmodified lines
modified:FAQ.md+20 added-3 removed
92 unmodified lines
93</h3>9495Yes! If you installed ripgrep through a package manager on a Unix system, then96the shell completion files included in the release archive should have been97installed for you automatically. If not, you can generate completes using97installed for you automatically. If not, you can generate completions using98ripgrep's command line interface.99100For **bash**:101
10 unmodified lines
112$ mkdir -p "$dir"113$ rg --generate complete-fish > "$dir/rg.fish"114```115116For **zsh**:116For **zsh**, the recommended approach is:117118```118```zsh119$ dir="$HOME/.zsh-complete"120$ mkdir -p "$dir"121$ rg --generate complete-zsh > "$dir/_rg"122```123124And then add `$HOME/.zsh-complete` to your `fpath` in, e.g., your125`$HOME/.zshrc` file:126127```zsh128fpath=($HOME/.zsh-complete $fpath)129```130131Or if you'd prefer to load and generate completions at the same time, you can132add the following to your `$HOME/.zshrc` file:133134```zsh135$ source <(rg --generate complete-zsh)136```137138Note though that while this approach is easier to setup, is generally slower139than the previous method, and will add more time to loading your shell prompt.140141For **PowerShell**, create the completions:142143```144$ rg --generate complete-powershell > _rg.ps1
919 unmodified lines