<!--
  canonical-url: https://www.avonture.be/blog/zsh-syntax-highlighting
  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.
-->

# Syntax highlighting in the console using ZSH

> Enhance your Zsh console with syntax highlighting! See commands turn green (valid) or red (invalid) as you type. Quick installation guide for zsh-syntax-highlighting.

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

**TL;DR**

This article introduces `zsh-syntax-highlighting`, a Zsh plugin that colors commands in your terminal as you type: green for valid, executable commands and red for typos or unknown ones. It walks through cloning the plugin into your Oh My Zsh custom plugins folder and adding it to the `plugins=(...)` list in `~/.zshrc`, with no further configuration needed to start using it.

[zsh-syntax-highlighting](https://github.com/zsh-users/zsh-syntax-highlighting/) is another gem for ZSH — assuming you've already installed Oh-My-Zsh (see [How to install Oh-My-ZSH](/blog/zsh-install) if not).

As you type, you'll be able to tell from the colors that, for example, something isn't quite right.

If you type `head` followed by a space, ZSH will display this word in green: this command exists and is valid.  If you type `heat` there, the word will appear in red: this command does not exist.

It sounds simple, but it's so practical.

## What Syntax Highlighting Looks Like

Type a command like `cat` or `head` and it turns green: the command exists, it's correctly typed and it's executable.

![Highlight in green](./images/head.webp)

Make a typo and the very same word turns red before you even press `ENTER`:

![Highlight in red](./images/docker_compose.webp)

That color appears **while you type**, not after the command has failed. You know you've mistyped `docekr` before losing a second to a `command not found`.&#x20;

## How to use it

In fact, nothing has to be done: there is no configuration file, no option to set, no alias to define. Install the plugin, reload your shell, and the coloring is simply there from the next character you type.

## Seeing It in Action with Docker

No history to seed, no config to write — this plugin needs nothing but itself, which makes it a
perfect one-command Docker test.

> **Why Docker first?:**
>
> The Dockerfile below installs Oh-My-Zsh and the plugin, and enables it in `~/.zshrc` — the exact
> two steps from the "Installation" section, done for you. Nothing on your own machine changes.

**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-syntax-highlighting and enable it in the default plugins=() list
RUN git clone --depth=1 https://github.com/zsh-users/zsh-syntax-highlighting \
        /root/.oh-my-zsh/custom/plugins/zsh-syntax-highlighting && \
    sed -i 's/^plugins=(git)/plugins=(git zsh-syntax-highlighting)/' /root/.zshrc

# 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 syntax-demo .
[+] Building 18.4s (7/7) FINISHED
✔ exporting to image
$ docker run --rm -it syntax-demo
🐳 root ~ # head
```

Type `head` and the word turns green as you finish typing it — the command exists. Backspace it,
type `heat` or `docekr` instead, and it turns red before you even press `ENTER`: same&#x20;
scene as the two screenshots above, live in your own terminal.

## Installation

Just clone the official repository like below:

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

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

**\~/.zshrc**

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

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

## Conclusion

Two lines of setup and your terminal starts proofreading you: green means "this will run", red means "fix me first". It's the kind of tiny feedback loop you stop noticing after a week — until you sit at a machine without it and start typing blind again.

Its natural companion is [Autosuggestions in the console using ZSH](/blog/zsh-plugin-autosuggestions): this one tells you whether what you're typing is valid, the other saves you from typing it at all.
