Skip to main content

Command Palette

Search for a command to run...

Less full of colors

Updated
3 min readView as Markdown

less is the workhorse of the terminal, but by default it dumps source files onto the screen as monochrome text. You can pipe files through a highlighter manually, but the nicer solution is to make it automatic: configure your shell so that every time you run less on a code file, GNU source-highlight colors it for you.

Based on Viewing more with less – color syntax by Vedran Vidovic, here's the permanent setup.

The Setup

The whole thing rests on two environment variables that less reads. First, install the highlighter (Debian/Ubuntu naming):

brew install source-highlight

Then append this to your ~/.zshrc (or ~/.profile if you prefer login-shell scope):

# less syntax highlighting + source-highlight installation
export LESSOPEN="| /opt/homebrew/Cellar/source-highlight/3.1.9_6/bin/src-hilite-lesspipe.sh %s"
export LESS=' -R'

Reload your shell config or open a new terminal:

source ~/.bashrc

From that point on, any file you open by name — less main.go, less config.json, less script.py — arrives already colorized. No wrapper aliases, no remembering extra flags; it just becomes the default behavior of less.

Why These Two Lines Work

LESSOPEN is the hook that does the heavy lifting. When less opens a file, it runs the command after the pipe symbol as an input preprocessor, with %s standing in for the filename. Here the preprocessor is src-hilite-lesspipe.sh, a small bridge script shipped with the source-highlight package that detects the file's language and emits colorized output for less to display.

LESS=' -R' is the necessary companion. Terminals show colors through raw ANSI escape sequences, and without -R, less would neutralize them into literal ^[[ gibberish. The -R flag tells less to pass raw control characters through so the colors actually render. Note the leading space in the value — it's the standard way of placing the option without disturbing any LESS value you might set elsewhere.

Caveats Worth Knowing

The automatic highlighting applies to files less opens by name, since that's what LESSOPEN receives — piping content in (cat file | less) bypasses the preprocessor. The script path is also distribution-specific; if you're not on Debian/Ubuntu, check where your package placed src-hilite-lesspipe.sh. And if a language isn't supported out of the box (YAML, for instance), source-highlight accepts custom language definitions that you can drop into its configuration directory.

It's a five-minute setup that quietly improves every future code-reading session in the terminal — and as the original article's screenshots show, the difference between highlighted and plain less output is immediately obvious the first time you open a JSON file.

Notice pipelining and redirect output between scripts does bring small increase of memory foorprint for less command, but hey! Colors are colors, no?