How to colorizing cat command output or files using ccat app on Linux or Unix system

Posted on in Categories Command Line Hacks, Howto last updated August 30, 2017

The cat command (short for concatenate) is one of the most frequently used flexible commands on Linux, Apple MacOS X, *BSD (FreeBSD / OpenBSD / NetBSD), Unix-like operating systems. It is useful to display text files on screen, copy text files, combine text files, create new text files and more. If you cat command with colours, try ccat app.

More about ccat

ccat is the colorizing cat. It works similar to the cat command but displays content with syntax highlighting. It supports JavaScript, Java, Ruby, Python, Go, C, and JSON programming languages. The overhead of ccat command comparing to a cat is minimum on a modern desktop with powerful multi-core CPUs and tons of RAM.

Installation

Install ccat as per your Linux/Unix distro.

Install ccat on macOS

Type the following brew command:
$ brew install ccat
Sample outputs:

==> Downloading https://homebrew.bintray.com/bottles/ccat-1.1.0.sierra.bottle.tar.gz
######################################################################## 100.0%
==> Pouring ccat-1.1.0.sierra.bottle.tar.gz
🍺  /usr/local/Cellar/ccat/1.1.0: 4 files, 3.5MB

Install ccat on Arch Linux

Type the following command:
$ sudo pacaur -S ccat

Generic installation method for Linux/Unix users

You must have golang installed on your system. Type the following command
$ go get -u github.com/jingweno/ccat

How do I use ccat command?

The syntax is as follows:
$ ccat fileNameHere
$ ccat /etc/passwd
$ ccat file.txt foo.txt bar.txt

To output in HTML

$ ccat --html file.txt

Set dark background

$ ccat --bg=dark input.txt
Set light background:
$ ccat --bg=light input.txt
You can set color codes too:
$ ccat -G String="_darkblue_" -G Plaintext="darkred" input
To see all possible values for color codes/palette run:
$ ccat --palette
Sample outputs:

Applied color codes:

       String	brown
      Keyword	darkblue
      Comment	lightgrey
         Type	teal
      Literal	teal
  Punctuation	darkred
    Plaintext	darkblue
          Tag	blue
      HTMLTag	lightgreen
 HTMLAttrName	blue
HTMLAttrValue	green
      Decimal	darkblue

Color code is in the format of:

  color       normal color
  *color*     bold color
  _color_     underlined color
  +color+     blinking color

Value of color can be black, blink, blue, bold, brown, darkblue, darkgray, darkgreen, darkred, darkteal, darkyellow, faint, fuchsia, fuscia, green, lightgray, overline, purple, red, reset, standout, teal, turquoise, underline, white, yellow

You can read input from stdin/keyboard as follows:
$ ccat
You need to press [CTRL] + [D] i.e. hold the control key down, then tap d to exit ccat session:

Fig.01: ccat command in action
Fig.01: ccat command in action

You can read source directly from the web too:
$ curl https://raw.githubusercontent.com/nixcraft/pf.conf/master/pf.conf | ccat
Sample outputs:
Fig.02: ccat command in action
Fig.02: ccat command in action

For more info see ccat project home page.

See also

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin and a trainer for the Linux operating system/Unix shell scripting. He has worked with global clients and in various industries, including IT, education, defense and space research, and the nonprofit sector. Follow him on Twitter, Facebook, Google+.

2 comment

Leave a Comment