The Best Autocomplete Plugin for ZSH
zsh-autosuggestions completes commands from your history as you type. How to install it on Oh My Zsh or Homebrew, and why the accept key is not Tab.
One of the greatest wonders of using ZSH is its customization flexibility, with a large number of plugins developed by users. About a year ago (in 2017) I found the best one, zsh-autosuggestions.
What it does
As you type, the plugin searches your command history and shows the rest of the most recent match in a muted gray, right after the cursor. Nothing runs and nothing is inserted into the line until you ask for it, so it stays out of the way when you are typing something new.
The history it reads is whatever $HISTFILE points to, which is ~/.zsh_history on Oh My Zsh and ~/.zhistory on Prezto. A command you never ran produces no suggestion, and that is the point: it repeats you, it does not guess.
Installing
On Oh My Zsh, clone it into the custom plugins directory and add it to the plugin list:
git clone https://github.com/zsh-users/zsh-autosuggestions \
${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions
# ~/.zshrc
plugins=(
# other plugins...
zsh-autosuggestions
)
With Homebrew, install the package and source it from your .zshrc:
brew install zsh-autosuggestions
source $(brew --prefix)/share/zsh-autosuggestions/zsh-autosuggestions.zsh
If you also use zsh-syntax-highlighting, source that one last. It wraps the widgets other plugins define and needs to find them already in place.
Accepting a suggestion
Press → to take the whole suggestion, or End, which does the same from anywhere on the line. Ctrl + → takes one word at a time, and that is the one I use most: the beginning of a command is usually right and the last argument usually is not.
Tab does not accept a suggestion. Tab is ZSH's completion system, a separate mechanism that expands paths, flags and arguments. The two run side by side and complete each other, which is exactly why people mix them up.
If the gray is too dark on your terminal theme, that is one line:
ZSH_AUTOSUGGEST_HIGHLIGHT_STYLE='fg=245'
The productivity boost is noticeable in the first few days. Long commands you already ran once, with the right flags, come back with a single keystroke.
This article was translated from Portuguese with the help of an LLM. The original version may contain nuances not fully captured in this translation.