Skip to main content

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.

· 4 min read
View raw
Christophe
Markdown, WSL and Docker lover ~ Insatiable curious.
Cet article existe aussi en français — lire la version française.

Changelog

Latest
added a Docker-first demo with a pre-seeded history to try before installing
updated plugins array; show only installed plugins

Autosuggestions in the console using ZSH

⚡
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 one — assuming you've already installed Oh-My-Zsh (see How to install Oh-My-ZSH 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

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.

Build and run it:

>user@machine: ~/autosuggestions-demo

$ 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:

>user@machine: ~/yourproject

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

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

This is a configuration snippet that defines a list of plugins to load in Zsh (a Unix shell). It includes the built-in git plugin, any other plugins you already had configured, and the zsh-autosuggestions plugin which provides command suggestions as you type.

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. And for a plugin in the same spirit, dedicated to your SSH hosts: SSH - Autosuggestions with ZSH.


Was this article helpful?
Did you try the steps in this article?