Skip to content

Commit

Permalink
zsh: support sourcing zsh completion dynamically
Browse files Browse the repository at this point in the history
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
  • Loading branch information
vegerot authored Dec 31, 2024
1 parent 79cbe89 commit 9430512
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
23 changes: 20 additions & 3 deletions FAQ.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ Does ripgrep have support for shell auto-completion?

Yes! If you installed ripgrep through a package manager on a Unix system, then
the shell completion files included in the release archive should have been
installed for you automatically. If not, you can generate completes using
installed for you automatically. If not, you can generate completions using
ripgrep's command line interface.

For **bash**:
Expand All @@ -113,14 +113,31 @@ $ mkdir -p "$dir"
$ rg --generate complete-fish > "$dir/rg.fish"
```

For **zsh**:
For **zsh**, the recommended approach is:

```
```zsh
$ dir="$HOME/.zsh-complete"
$ mkdir -p "$dir"
$ rg --generate complete-zsh > "$dir/_rg"
```

And then add `$HOME/.zsh-complete` to your `fpath` in, e.g., your
`$HOME/.zshrc` file:

```zsh
fpath=($HOME/.zsh-complete $fpath)
```

Or if you'd prefer to load and generate completions at the same time, you can
add the following to your `$HOME/.zshrc` file:

```zsh
$ source <(rg --generate complete-zsh)
```

Note though that while this approach is easier to setup, is generally slower
than the previous method, and will add more time to loading your shell prompt.

For **PowerShell**, create the completions:

```
Expand Down
10 changes: 9 additions & 1 deletion crates/core/flags/complete/rg.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,15 @@ _rg_types() {
fi
}

_rg "$@"
# Don't run the completion function when being sourced by itself.
#
# See https://github.com/BurntSushi/ripgrep/issues/2956
# See https://github.com/BurntSushi/ripgrep/pull/2957
if [[ $funcstack[1] == _rg ]] || (( ! $+functions[compdef] )); then
_rg "$@"
else
compdef _rg rg
fi

################################################################################
# ZSH COMPLETION REFERENCE
Expand Down

0 comments on commit 9430512

Please sign in to comment.