50

Sometimes I quickly want to view the contents of a file from the command line. For this I of course use cat, but it is often source files in Python, Java or simple HTML. For these files it would be handy if cat could give some color markup to the files, so that it reads more easy.

Can cat do such a thing?

kramer65
  • 2,193

9 Answers9

54

cat is not able to do this. However, maybe pygments may be able to help you there. It is a python script and can be either installed via apt-get

sudo apt-get install python-pygments

or easily downloaded and installed via easy_install.

It supports lots of source code languages and also markup languages

It is used by

pygmentize -g <filename>
txwikinger
  • 29,406
19

Not from cat itself but you can use something like source highlite or supercat or highlight

Source-highlight

This program, given a source file, produces a document with syntax highlighting. It also provides a C++ highlight library (new) (since version 3.0).

Source-highlight reads source language specifications dynamically, thus it can be easily extended (without recompiling the sources) for handling new languages. It also reads output format specifications dynamically, and thus it can be easily extended (without recompiling the sources) for handling new output formats. The syntax for these specifications is quite easy (take a look at the manual).

The manual about installation:

See the file INSTALL for detailed building and installation instructions; anyway if you're used to compiling Linux software that comes with sources you may simply follow the usual procedure, i.e., untar the file you downloaded in a directory and then:

 cd <source code main directory>
 ./configure
 make
 make install

Supercat

This is Supercat's homepage. Supercat is a program that colorizes text based on matching regular expressions/strings/characters. Supercat supports html output as well as standard ASCII text. Unlike some text-colorizing programs that exist, Supercat does not require you to have to be a programmer to make colorization rules.

If you have written a supercat config file for a standard file type please do not hesitate to contact me at "bug-spc (at) nosredna (dot) net" for possible inclusion in the supercat distribution.

Or with a function (source):

#!/bin/bash#!/bin/bash
if [ ! -t 0 ];then
        file=/dev/stdin
elif [ -f $1 ];then
        file=$1
else
        echo "Usage: $0 code.c"
        echo "or e.g. head code.c|$0"
        exit 1
fi
pygmentize -f terminal -g $file

Requires: Pygments (sudo apt-get install python-pygments python3-pygments) Add it as a function to bash .functions and give it a name like color()

Rinzwind
  • 309,379
16

As from this answer here, you can use the python-pygments packages to highlight stuff. First do:

sudo apt-get install python-pygments python3-pygments

then:

pygmentize -g FILENAME

then have a go:

enter image description here

You can also set it as an alias, like in the answer I linked - basically, run this:

echo "alias catc='pygmentize -g'" >> ~/.bash_aliases 
chmod +x ~/.bash_aliases

Close the terminal, open it again, and the catc command should now work - if it does not, make sure these lines are in the .bashrc file, and are uncommented:

if [ -f ~/.bash_aliases ]; then
. ~/.bash_aliases
fi

Another thing would be to just use nano:

nano testfile

enter image description here

Wilf
  • 30,732
7

man view or man vim

Basic usage: view <filename>

Quit: :q<Return> (add an <Esc> first if using vim), or ZZ (upper case z twice).

The programmer's text editor vim has all you need already, and is likely already part of your system.

vim has a read-only mode activated with view or vim -R. If all you want to do is view the marked-up file, it should be enough.

Simple to use, navigable, available everywhere. No need to mess about with installing new software or writing bash scripts.

4

Bat — A cat clone with wings

You might also want to check out bat which has the following features:

  • Syntax highlighting
  • Git integration
  • Optionally showing non-printable characters
  • Automatic paging with less
  • File concatenation as a drop-in replacement for cat when redirected

 Preview

Screenshot of bat showing a colored file

Picture taken from the official GitHub

Installation

You can get the latest Debian package here and install it with:

sudo dpkg -i bat_0.10.0_amd64.deb

Adapting the version number and architecture.

4

One can check out ccat.

It adds syntax highlight to output files.

Alan Dong
  • 141
3

cat can not produce syntax highlighting solely. Still you can do this as follows, using python-pygments. First install it from terminal as,

sudo apt-get install python-pygments

Now copy the function below ~/.bashrc. It will give you what you want moreover it will preserve the properties of cat otherwise there is no point of using cat

catc(){
    cat "$@" > /tmp/.tmp
    pygmentize -g /tmp/.tmp
    rm /tmp/.tmp
}

Source ~/.bashrc as,

. ~/.bashrc

It will give colourized output,

catc <filename>

It will concatenate with color as well,

catc <file1> <file2> ... <filen>
sourav c.
  • 46,120
0

Rather than installing a third party package, you can simply use gedit to quickly view a file with syntax highlighting. For example copy the address bar above and paste into your terminal:

gedit https://askubuntu.com/questions/405960/can-cat-show-files-using-code-markup-in-colors

You will see this question in HTML with formatted colors:

gedit https.png

  • You can use familiar gedit navigation keys
  • You can turn line wrap on/off
  • You can use plugins like I have installed for 80 character gutter and document overview (far right) with thumbnail slider
  • The only caveat is you need to remember Alt+F4 to quickly close the window to simulate cat which doesn't require keystrokes to close.
0

Other answers cover why cat is not able to do it. Though you can do it with less using lesspipe.sh.

Fuad Saud
  • 180