Zsh autosuggestions don’t actually use your command history to suggest commands; they predict your next command based on the characters you’ve already typed.

Imagine this: you’re at the terminal, fingers poised. You type git c. Instantly, a faint gray suggestion appears: git checkout.

% git c<--git checkout-->

This isn’t magic, nor is it just a lookup in your ~/.zsh_history file. It’s a real-time prediction engine running as you type.

Let’s see it in action. First, you need to install zsh-autosuggestions. The most common way is with a package manager like Homebrew:

brew install zsh-autosuggestions

Once installed, you need to load it into your ~/.zshrc file. Add this line:

source $(brew --prefix)/share/zsh-autosuggestions/zsh-autosuggestions.zsh

After sourcing your ~/.zshrc (either by running source ~/.zshrc or opening a new terminal session), you’ll start seeing suggestions. Type ls -l and then cd into a directory, and as you type ls again, you’ll see ls -l as a suggestion. Type git st and git status will likely appear.

The core mechanism is a state machine that analyzes the characters you type and compares them against a large, pre-computed set of command patterns. It’s not just matching prefixes; it’s about predicting the most likely next token, or sequence of tokens, given the current input and the context of commands you’ve run previously. The suggestions are drawn from a "history" that’s constantly being updated and analyzed.

You can customize how these suggestions appear and behave. The most common configuration options are set via environment variables in your ~/.zshrc file, before the source line.

For example, to change the color of the suggestions from the default gray to a bright yellow, you’d add:

ZSH_AUTOSUGGEST_HIGHLIGHT_COLOR="yellow"

If you want to be more aggressive with suggestions and have them appear even for commands you’ve only run once, you can adjust the strategy. The default is history, which is pretty good. But you can try subsequence for more general pattern matching:

ZSH_AUTOSUGGEST_STRATEGY="subsequence"

This might give you more suggestions, but potentially less relevant ones, as it’s looking for any sequence that matches what you’ve typed, not just full commands.

You can also control the completion strategy. ZSH_AUTOSUGGEST_COMPLETION_STRATEGY can be set to exact or substring. exact will only suggest commands that exactly match what you’ve typed so far. substring will offer suggestions where your typed text is a substring within the suggested command.

A particularly useful setting is ZSH_AUTOSUGGEST_IGNORE_SUBSEQUENT_DUPLICATES. If you have git push origin main in your history, and you type git push, it might suggest git push origin main. If you then type git push origin, it won’t suggest git push origin main again if it has already been suggested for git push. This prevents redundant suggestions.

ZSH_AUTOSUGGEST_IGNORE_SUBSEQUENT_DUPLICATES=1

The suggestions are rendered using Zsh’s zle (Zsh Line Editor) widgets. When you press the right arrow key (or Ctrl+F by default), the current suggestion is accepted and inserted into your command line. This is why it feels so integrated – it’s using the same line editing mechanisms that handle tab completion and command history navigation.

Many users find that autosuggestions can sometimes be too eager, predicting commands that aren’t quite what they intended. A subtle but powerful way to temper this is by configuring how zsh-autosuggestions interacts with fuzzy completion. By default, it tries to provide suggestions even when you’re using fuzzy matching for commands or file paths. If you find this overwhelming, you can tell it to hold back.

ZSH_AUTOSUGGEST_USE_ASYNC=0

Disabling asynchronous suggestion generation might seem counter-intuitive, but it can sometimes lead to a more predictable and less "hyperactive" suggestion experience, especially on slower systems or with very large histories. The suggestions will still appear, but they might take a fraction of a second longer, and the system might be less likely to jump in with a suggestion when you’re actively typing a complex command.

The real power of zsh-autosuggestions lies in its ability to learn your patterns and anticipate your needs without you explicitly configuring aliases or complex scripts. It’s a dynamic, context-aware assistant that adapts to your unique command-line habits.

The next thing you’ll probably want to tackle is integrating syntax highlighting, which often goes hand-in-hand with autosuggestions for a truly enhanced shell experience.

Want structured learning?

Take the full Zsh course →