Skip to content

Commit

Permalink
Merge pull request #18524 from HeroCC/shellenv-fpath
Browse files Browse the repository at this point in the history
shellenv: Add zsh site-functions to `fpath`
  • Loading branch information
ZhongRuoyu authored Oct 14, 2024
2 parents d6591d4 + 94426ba commit 91d6ede
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 13 deletions.
4 changes: 4 additions & 0 deletions Library/Homebrew/cmd/shellenv.sh
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ homebrew-shellenv() {
echo "export HOMEBREW_PREFIX=\"${HOMEBREW_PREFIX}\";"
echo "export HOMEBREW_CELLAR=\"${HOMEBREW_CELLAR}\";"
echo "export HOMEBREW_REPOSITORY=\"${HOMEBREW_REPOSITORY}\";"
if [[ "${HOMEBREW_SHELL_NAME}" == "zsh" ]] || [[ "${HOMEBREW_SHELL_NAME}" == "-zsh" ]]
then
echo "fpath[1,0]=\"${HOMEBREW_PREFIX}/share/zsh/site-functions\";"
fi
if [[ -n "${PATH_HELPER_ROOT}" ]]
then
PATH_HELPER_ROOT="${PATH_HELPER_ROOT}" PATH="${HOMEBREW_PATH}" /usr/libexec/path_helper -s
Expand Down
17 changes: 4 additions & 13 deletions docs/Shell-Completion.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,23 +34,14 @@ If you are using Homebrew's `bash` as your shell (i.e. `bash` >= v4) you should

## Configuring Completions in `zsh`

To make Homebrew's completions available in `zsh`, you must insert the Homebrew-managed `zsh/site-functions` path into your `FPATH` before initialising `zsh`'s completion facility. Add the following to your `~/.zshrc`:
To make Homebrew's completions available in `zsh`, the Homebrew-managed `zsh/site-functions` path needs to be inserted into `FPATH` before initialising `zsh`'s completion facility. This is done by `brew shellenv`, so if you followed the post-Homebrew installation steps, `eval "$(brew shellenv)"` should be in your `~/.zprofile` (on macOS) or `~/.zshrc` (on Linux). All you need is add the following to your `~/.zshrc` if it's not already there, and, if you're on Linux, make sure it's placed after `eval "$(brew shellenv)"`:

```sh
if type brew &>/dev/null
then
FPATH="$(brew --prefix)/share/zsh/site-functions:${FPATH}"

autoload -Uz compinit
compinit
fi
autoload -Uz compinit
compinit
```

This must be done before `compinit` is called. Note that if you are using Oh My Zsh, it will call `compinit` for you when you source `oh-my-zsh.sh`. In this case, instead of the above, add the following line to your `~/.zshrc`, before you source `oh-my-zsh.sh`:

```sh
FPATH="$(brew --prefix)/share/zsh/site-functions:${FPATH}"
```
Note that if you are using Oh My Zsh, it will call `compinit` for you when you source `oh-my-zsh.sh`. In this case, make sure `eval "$(brew shellenv)"` is called before sourcing `oh-my-zsh.sh` if you're on Linux, and you should be all set without any additional configuration.

You may also need to forcibly rebuild `zcompdump`:

Expand Down

0 comments on commit 91d6ede

Please sign in to comment.