Syntax highlighting/colorizing cat Syntax highlighting/colorizing cat unix unix

Syntax highlighting/colorizing cat


I'd recommend pygmentize from the python package python-pygments. You may want to define the following handy alias (unless you use ccat from the ccrypt package).

alias ccat='pygmentize -g'

Syntax highlighted cat output using pygmentize

And if you want line numbers:

alias ccat='pygmentize -g -O style=colorful,linenos=1'


Options:

pygmentize is good. I have an alias:

alias c='pygmentize -g'

but highlight is another widely available alternative is

alias cats='highlight -O ansi --force'

Installation:

You may have to install pygments using one of these:

sudo apt install python-pygmentssudo pip install pygmentssudo easy_install Pygments #for Mac user

and for highlight package which is easily available on all distributions

sudo apt install highlightsudo yum install highlight

In Action:

I'm attaching shots for both down below for a good comparison in highlightings

Here is pygmentize in action:Pygmentize highlighting on python file

and this is highlight:Highlight highlighting on python file


From late April 2018 onwards:

Bat - A cat(1) clone with syntax highlighting and Git integration.

The project is a cat clone with support for colors and customizations written in Rust. It offers not only syntax highlighting with multiple themes, but also Git integration. As described in the documentation:

bat tries to achieve the following goals:

  • Provide beautiful, advanced syntax highlighting
  • Integrate with Git to show file modifications
  • Be a drop-in replacement for (POSIX) cat
  • Offer a user-friendly command-line interface

It is, needless to say, much faster than pygmentize and does not choke when confronted with large files.

Source code and binary releases + installation instructions can be found in the Github repository, as well as a comparison to alternative programs.