<!--
  canonical-url: https://www.avonture.be/blog/zsh-plugin-autosuggestions
  published:     2024-03-29
  This is a static plain-Markdown mirror generated at build time.
  Visit the canonical URL above for the fully rendered page, with images and interactive components.
-->

# Autosuggestions in the console using ZSH

> Boost your ZSH console efficiency. Install the zsh-autosuggestions plugin easily and get intelligent command suggestions from your history as you type.

---
![Autosuggestions in the console using ZSH](/img/v2/zsh.webp)

**TL;DR**

This article covers `zsh-autosuggestions`, a Zsh plugin that suggests commands from your shell history as you type, shown in gray text you can accept with `TAB`. It explains how to install the plugin by cloning it into your Oh My Zsh custom plugins folder and enabling it in `~/.zshrc`, then shows how to accept, cycle through, or ignore suggestions using `TAB` and the arrow keys.

ZSH supports plugins, and one of the wonders is the [zsh-autosuggestions](https://github.com/zsh-users/zsh-autosuggestions) one — assuming you've already installed Oh-My-Zsh (see [How to install Oh-My-ZSH](/blog/zsh-install) if not).

That one will suggest commands as you type, based on your previous history and completions. Three keystrokes, and the forty-character command you painfully assembled last month is sitting there, waiting for a `TAB`.

## Three Characters, and the Command Is Already There

Imagine you've already typed, today, yesterday or weeks ago, the `docker compose up --detach` command. Today, you type `doc` and here is what your console shows:

![Autosuggestions plugin for ZSH](./images/autosuggestions.webp)

The gray part is the suggestion. Press `TAB` and the whole line is yours; keep typing and the suggestion updates at every character; ignore it and nothing happens.

You can also play with `UP` and `DOWN` to switch to other commands matching the same keys (*doc* in my example).

## Why it works

* Suggestions come straight from your Linux HISTORY: no index to build, no configuration, nothing to teach the plugin.
* The more you use your console, the better the suggestions get — it learns from you and only from you.
* No more wondering, "Gee, what were the parameters I used for ...".

## Seeing It in Action with Docker

You don't need Oh-My-Zsh already running on your machine to see the plugin work — a throwaway
container does the job.

> **Why Docker first?:**
>
> The Dockerfile below installs Oh-My-Zsh, clones the plugin, enables it in `~/.zshrc`, and — the
> part that matters — pre-seeds a shell history containing `docker compose up --detach`. Type `doc`
> and the exact scenario from the screenshot above appears, with nothing to configure yourself.

**Dockerfile**

```docker
FROM ubuntu:24.04

ENV DEBIAN_FRONTEND=noninteractive

RUN apt-get update && \
    apt-get install -y --no-install-recommends \
        zsh \
        git \
        curl \
        ca-certificates && \
    rm -rf /var/lib/apt/lists/*

# Install Oh-My-Zsh unattended (host framework required by the plugin)
RUN sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)" \
    "" --unattended --keep-zshrc

# Install zsh-autosuggestions and enable it in the default plugins=() list
RUN git clone --depth=1 https://github.com/zsh-users/zsh-autosuggestions \
        /root/.oh-my-zsh/custom/plugins/zsh-autosuggestions && \
    sed -i 's/^plugins=(git)/plugins=(git zsh-autosuggestions)/' /root/.zshrc

# Pre-seed a shell history so a suggestion is already there to accept: type
# "doc" and the full `docker compose up --detach` line appears in gray text.
RUN printf '%s\n' \
    ': 1700000000:0;docker compose up --detach' \
    ': 1700000100:0;docker compose logs --follow' \
    ': 1700000200:0;docker compose down --volumes' \
    > /root/.zsh_history

# Signal clearly that this shell runs inside a container
RUN echo "PROMPT='%B%F{yellow}🐳 %F{cyan}%n%f%b %~ %# '" >> /root/.zshrc

CMD ["/bin/zsh", "-i", "-l"]
```

Build and run it:

```bash
$ docker build -t autosuggestions-demo .
[+] Building 19.8s (8/8) FINISHED
✔ exporting to image
$ docker run --rm -it autosuggestions-demo
🐳 root ~ #
```

Now just type `doc` (don't press `ENTER` yet) and the exact scene from the screenshot at
the top of this article plays out: `docker compose up --detach` fills in as gray, ghosted text
right after your cursor — read straight from the pre-seeded history. Press `TAB` to
accept it, or `↑`/`↓` to cycle to the other two `docker compose` commands also
sitting in that history.

## Installation

Just clone the official repository like below:

```bash
$ `git clone https://github.com/zsh-users/zsh-autosuggestions \${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions`
```

Then edit the `~/.zshrc` file, search for `plugins=(` and add `zsh-autosuggestions` to the list. You'll have f.i. something like:

**\~/.zshrc**

```bash
plugins=(
  git
  # ... your existing plugins
  zsh-autosuggestions
)
```

Close your console, open a new one (or run `source ~/.zshrc` to load your change) and it's done.

## Conclusion

One `git clone`, one line in `~/.zshrc`, and your terminal stops asking you to remember flags you typed three weeks ago. Because everything is read from your history, the plugin is worth more every day you use it.

Since suggestions are only as good as what's stored, it's worth knowing how that history is kept and filtered: see [Linux - Working with the history of your last fired actions](/blog/linux-history). And for a plugin in the same spirit, dedicated to your SSH hosts: [SSH - Autosuggestions with ZSH](/blog/zsh-plugin-ssh-config-suggestions).
