Linux Tutorials

How To Enable Zsh Syntax Highlighting on Linux and macOS

A mistyped command should look wrong before you press Enter. That is what zsh-syntax-highlighting gives you: the command line is colored live as you type, green for commands that exist, red for ones that do not, yellow for quoted strings, underlines for paths that actually resolve. Typos die at the prompt instead of in your history.

Original content from computingforgeeks.com - post 3965

This guide covers every way to get zsh syntax highlighting working: the distro package on Ubuntu, Debian, Fedora and Arch, Homebrew on macOS, a plain git clone for systems with no package, and the Oh My Zsh plugin route. It also enables the extra highlighters and custom colors most setups never touch. You need zsh installed first, so start with the zsh and Oh My Zsh setup guide if you are still on bash. Everything below was run in August 2026 on Ubuntu 26.04 with the plugin’s current 0.8.0 release, with Fedora 44, Arch and Rocky Linux verified in containers and the Homebrew route tested on macOS with zsh 5.9.

Install Zsh Syntax Highlighting From Your Package Manager

The plugin is packaged by every major distro, so a one-line install beats cloning the repository for most machines. On Ubuntu and Debian:

sudo apt update
sudo apt install zsh zsh-syntax-highlighting

On Fedora:

sudo dnf install zsh zsh-syntax-highlighting

On Arch Linux:

sudo pacman -S zsh zsh-syntax-highlighting

On Rocky Linux 9 and AlmaLinux 9 the package lives in EPEL, and the epel-release package that enables it sits in the base repos:

sudo dnf install epel-release
sudo dnf install zsh zsh-syntax-highlighting

RHEL 9 proper has no epel-release in its repos, so subscribe to EPEL directly from Fedora and enable the CodeReady Builder repo it expects:

sudo subscription-manager repos --enable codeready-builder-for-rhel-9-$(arch)-rpms
sudo dnf install https://dl.fedoraproject.org/pub/epel/epel-release-latest-9.noarch.rpm
sudo dnf install zsh zsh-syntax-highlighting

The versions and install paths differ between distros, and the path matters because you will source the script by its absolute location in a moment. This is what each platform shipped when we checked:

PlatformPlugin versionScript location
Ubuntu 26.040.8.0/usr/share/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh
Ubuntu 24.040.7.1/usr/share/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh
Debian 130.8.0/usr/share/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh
Fedora 440.8.0/usr/share/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh
Arch Linux0.8.0/usr/share/zsh/plugins/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh
RHEL / Rocky / Alma 9 (EPEL)0.8.0/usr/share/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh
RHEL / Rocky / Alma 10not packageduse the git clone method below
macOS (Homebrew)0.8.0$(brew --prefix)/share/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh

Two rows deserve attention. Arch installs to /usr/share/zsh/plugins/, not the path every Debian-flavored tutorial shows, and EPEL 10 does not carry the package at all as of August 2026, so the RHEL 10 family falls through to the git method.

Install on macOS With Homebrew

macOS has shipped zsh as the default shell since Catalina, so the only missing piece is the plugin itself:

brew install zsh-syntax-highlighting

The formula lands in the Homebrew prefix, which is /opt/homebrew on Apple silicon and /usr/local on Intel Macs:

==> Summary
🍺  /opt/homebrew/Cellar/zsh-syntax-highlighting/0.8.0: 28 files, 210KB

Because the prefix differs by architecture, use $(brew --prefix) in the source line instead of hardcoding either path. The enable step below handles it. If your Mac terminal is still bare zsh, the Oh My Zsh on macOS walkthrough pairs well with this plugin.

Clone the Plugin From GitHub

The git method works on anything with zsh, including RHEL 10 systems with no package and servers where you cannot install system-wide. Clone into a hidden directory in your home:

git clone --depth 1 https://github.com/zsh-users/zsh-syntax-highlighting.git ~/.zsh/zsh-syntax-highlighting

The master branch identifies itself as a development build of the next release, which in practice is fine: it is what Oh My Zsh users run too. To update later, pull instead of recloning:

git -C ~/.zsh/zsh-syntax-highlighting pull

Enable the Plugin in Your .zshrc

One rule governs this step: the source line goes at the end of ~/.zshrc. The plugin hooks into the Zsh Line Editor and must register after every other widget and plugin has loaded. Sourcing it early is the top reason highlighting silently misbehaves. Append the line matching your install method:

echo "source /usr/share/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh" >> ~/.zshrc

On Arch, swap in the /usr/share/zsh/plugins/ path from the table. For a Homebrew install on macOS:

echo 'source $(brew --prefix)/share/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh' >> ~/.zshrc

For a git clone:

echo "source ~/.zsh/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh" >> ~/.zshrc

Open a new terminal or replace the running shell, then confirm the plugin loaded by reading the version variable it exports:

exec zsh
echo $ZSH_HIGHLIGHT_VERSION

The Ubuntu package answers with its patched version string:

0.8.0-2build1_ubuntu

From here every line you type is colored as you type it. Valid commands render green, unknown ones red, double and single quoted strings yellow, reserved words like for and done stand out, and existing paths get underlined:

Zsh syntax highlighting showing green valid commands, red unknown command and yellow quoted string

The red sl would have failed, and the shell told you before you ran it. That is the entire value of the plugin, delivered by one line of configuration.

Enable It as an Oh My Zsh Plugin

Oh My Zsh does not bundle zsh-syntax-highlighting, so clone it into the custom plugins directory where Oh My Zsh looks for third-party plugins:

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

Then edit the plugins array in ~/.zshrc:

vim ~/.zshrc

Add it as the last entry, since Oh My Zsh sources plugins in the order they are listed and the same load-last rule applies:

plugins=(git zsh-syntax-highlighting)

Restart the shell with exec zsh and the colors come up. If you also run the autosuggestions plugin, the setup steps in the zsh autosuggestions guide follow the same clone-and-list pattern, and the two plugins coexist cleanly with highlighting listed last.

Turn On More Highlighters

The coloring you have seen so far comes from a single highlighter called main, the only one active by default. The package ships seven: main, brackets, cursor, line, pattern, regexp and root. The two worth enabling on most machines are brackets, which colors nested parentheses by depth and flags unbalanced ones, and pattern, which lets you paint arbitrary command patterns any way you want.

Open your ~/.zshrc:

vim ~/.zshrc

Declare the highlighter list above the source line, and pattern rules after it. This block warns loudly whenever a command starts with rm -rf:

ZSH_HIGHLIGHT_HIGHLIGHTERS=(main brackets pattern)

source /usr/share/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh

typeset -A ZSH_HIGHLIGHT_PATTERNS
ZSH_HIGHLIGHT_PATTERNS+=('rm -rf *' 'fg=white,bold,bg=red')

Reload with exec zsh and type a destructive command without running it. The whole phrase lights up white on red, brackets pair up in matching colors, and everything else keeps the main highlighter’s behavior:

Zsh pattern highlighter marking rm -rf in red with colored bracket nesting and custom path style

That red banner has stopped me from wiping the wrong directory more than once. In production, you’ll want the pattern rule on every box you touch over SSH.

Customize the Highlight Colors

Every token type the main highlighter recognizes has an entry in the ZSH_HIGHLIGHT_STYLES associative array, and you can override any of them. Place overrides after the source line, because the plugin itself declares the array when it loads:

ZSH_HIGHLIGHT_STYLES[alias]='fg=magenta,bold'
ZSH_HIGHLIGHT_STYLES[path]='fg=cyan'
ZSH_HIGHLIGHT_STYLES[globbing]='none'

The first line makes your aliases visually distinct from real binaries, the second swaps the default underline on paths for cyan text (visible in the screenshot above), and the third turns off glob highlighting entirely. The style syntax matches the zsh zle_highlight format, and the highlighters documentation lists all forty-plus token types you can restyle. If you are theming the prompt itself rather than the command line, that is Powerlevel10k territory, and the two work together without conflict.

Errors You Can Hit

bad math expression: operand expected

This appears at shell startup when a ZSH_HIGHLIGHT_STYLES assignment sits above the source line in .zshrc:

/home/jmutai/.zshrc:1: bad math expression: operand expected at `/usr/local...'

The array does not exist yet at that point, so zsh treats the subscript as arithmetic and chokes. The fix is to move every style assignment below the source line. Declaring the array yourself first with typeset -A ZSH_HIGHLIGHT_STYLES also works, but ordering them correctly is cleaner.

no such file or directory on the source line

Copying a source line from a tutorial written for a different distro produces this on every new shell:

/home/jmutai/.zshrc:source:1: no such file or directory: /usr/share/zsh/plugins/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh

That is the Arch path failing on Ubuntu. Check where your package actually put the script and fix the path in .zshrc:

dpkg -L zsh-syntax-highlighting | grep 'zsh-syntax-highlighting.zsh$'

On Fedora and RHEL family systems the equivalent is rpm -ql zsh-syntax-highlighting, and on Arch pacman -Ql zsh-syntax-highlighting. While you are debugging startup errors, tab completion failures like _get_comp_words_by_ref not found come from the same class of .zshrc ordering mistakes.

Keep It Fast on Long Buffers

Highlighting recomputes on every keystroke, which you will never notice on normal commands but can feel when you paste a few hundred lines of script into the terminal. The plugin has a cutoff for exactly that case. Add it anywhere in your .zshrc:

ZSH_HIGHLIGHT_MAXLENGTH=512

Anything longer than 512 characters skips highlighting entirely. The main highlighter also stats paths as you type to decide whether to underline them, which gets slow when the path sits on a sluggish NFS or SMB mount. Blacklist those mount points:

ZSH_HIGHLIGHT_DIRS_BLACKLIST+=(/mnt/nfs-share)

With those two lines the plugin stays invisible on the slow paths and instant everywhere else, which is how a shell plugin should behave. Set it up once, put the source line last, and forget it is there until the first red command saves you.

Keep reading

Upgrade Ubuntu 24.04 to Ubuntu 26.04 LTS (Step by Step) Ubuntu Upgrade Ubuntu 24.04 to Ubuntu 26.04 LTS (Step by Step) UFW Firewall Commands with Examples on Ubuntu 24.04 / 22.04 Security UFW Firewall Commands with Examples on Ubuntu 24.04 / 22.04 Things to Do After Installing CachyOS Arch Linux Things to Do After Installing CachyOS Install Arcane on Ubuntu 26.04 / 24.04: Complete Docker UI Guide Containers Install Arcane on Ubuntu 26.04 / 24.04: Complete Docker UI Guide Install NVIDIA Drivers and CUDA Toolkit on Ubuntu 26.04 / 24.04 Ubuntu Install NVIDIA Drivers and CUDA Toolkit on Ubuntu 26.04 / 24.04 How To Install Jellyfin on Ubuntu 24.04 Ubuntu How To Install Jellyfin on Ubuntu 24.04

Leave a Comment

Press ESC to close