brew/docs/Shell-Completion.md
Matthew Rothenberg dc30e9f64d
speedup bash completion loading suggestion in docs
The current suggestion for users shells out to `brew --prefix` three separate times, which has a non-zero overhead (as per quick benchmark below, approx 20ms per invocation). By making a minor change, the first invocation can be stored in a local variable to reduce shell startup time.

```
$ hyperfine --warmup=3 'brew --prefix'       
Benchmark #1: brew --prefix
  Time (mean ± σ):      21.3 ms ±   1.5 ms    [User: 8.8 ms, System: 10.6 ms]
  Range (min … max):    20.3 ms …  37.0 ms    128 runs
```
2019-05-07 12:36:02 -04:00

54 lines
2.1 KiB
Markdown

# Homebrew Shell Completion
Homebrew comes with completion definitions for the `brew` command. Some packages also provide completion definitions for their own programs.
`zsh`, `bash` and `fish` are currently supported. (Homebrew provides `brew` completions for `zsh` and `bash`; `fish` provides its own `brew` completions.)
You must configure your shell to enable the completion support. This is because the Homebrew-managed completions are stored under `HOMEBREW_PREFIX`, which your system shell may not be aware of, and because it is difficult to automatically configure `bash` and `zsh` completions in a robust manner, so the Homebrew installer cannot do it for you.
## Configuring Completions in `bash`
To make Homebrew's completions available in `bash`, you must source the definitions as part of your shell startup. Add the following to your `~/.bash_profile` file:
```sh
HOMEBREW_PREFIX=$(brew --prefix)
if type brew &>/dev/null; then
for COMPLETION in "$HOMEBREW_PREFIX"/etc/bash_completion.d/*
do
[[ -f $COMPLETION ]] && source "$COMPLETION"
done
if [[ -f ${HOMEBREW_PREFIX}/etc/profile.d/bash_completion.sh ]];
then
source "${HOMEBREW_PREFIX}/etc/profile.d/bash_completion.sh"
fi
fi
```
## Configuring Completions in `zsh`
To make Homebrew's completions available in `zsh`, you must get the Homebrew-managed zsh site-functions on your `FPATH` before initialising `zsh`'s completion facility. Add the following to your `~/.zshrc` file:
```sh
if type brew &>/dev/null; then
FPATH=$(brew --prefix)/share/zsh/site-functions:$FPATH
fi
```
This must be done before `compinit` is called. Note that if you are using Oh My Zsh, it will call `compinit` for you, so this must be done before you call `oh-my-zsh.sh`.
You may also need to forcibly rebuild `zcompdump`:
```sh
rm -f ~/.zcompdump; compinit
```
Additionally, if you receive "zsh compinit: insecure directories" warnings when attempting to load these completions, you may need to run this:
```sh
chmod go-w "$(brew --prefix)/share"
```
## Configuring Completions in `fish`
No configuration is needed in `fish`. Friendly!