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

feat(completion): support sourcing zsh completion dynamically #2957

Merged
merged 2 commits into from
Dec 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"
```
vegerot marked this conversation as resolved.
Show resolved Hide resolved

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

```zsh
fpath=($HOME/.zsh-complete $fpath)
```
vegerot marked this conversation as resolved.
Show resolved Hide resolved

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
Loading