Syntax highlighting/colorizing cat

Solution 1:

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'

Solution 2:

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-pygments
sudo pip install pygments
sudo easy_install Pygments #for Mac user

and for highlight package which is easily available on all distributions

sudo apt install highlight
sudo yum install highlight
  • Bitbucket repo: https://bitbucket.org/birkenfeld/pygments-main
  • GitHub mirror: https://github.com/sglyon/pygments

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

Solution 3:

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.

Solution 4:

vimcat is single-file (shell script) and works good:

http://www.vim.org/scripts/script.php?script_id=4325

Last update is from December 2013. Hint: you can force file type recognition by vimcat -c "set ft=<type>".