Sign up ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free.

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?

share|improve this question
This may already have an answer here –  Wilf Jan 15 '14 at 16:58
ha @wilf I found the same code with google :D Also worth noting this topic on SO:… –  Rinzwind Jan 15 '14 at 17:07
@Rinzwind - I searched stackoverflow , as it was bound to come up ;-) –  Wilf Jan 15 '14 at 17:09
My cat can do that. She is always getting random files from my desk. Depending how hard she licks, the file has a different color afterwards. –  Assylum Jan 16 '14 at 4:36
This question reminds me of cat -v considered harmful. –  Snowball Jan 16 '14 at 5:39

7 Answers 7

up vote 23 down vote accepted

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>
share|improve this answer
Open file as pygmentize -g <filename>, the -g switch is important to handle file without lexer. for example if you try /etc/fstab to open without -g it will fail. –  souravc Jan 15 '14 at 18:25
Thanks @souravc Added it to the answer. Interestingly, this argument does not seem to be mentioned in the man page or help page –  txwikinger Jan 15 '14 at 18:29
Well I played a lot with it. you can find it, try pygmentize --help If -g is passed, attempt to guess the lexer from the file contents, or pass through as plain text if this fails (this can work for stdin). –  souravc Jan 15 '14 at 18:33
This is the best, then alias cat to pygmentize -g and you are golden. In fish it is simple as funced cat... type pygmentize -g $argv... save..funcsave cat. –  Elijah Lynn Jun 28 at 13:04

Not from cat itself but you can use something like source highlite or supercat or 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>
 make install


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):

if [ ! -t 0 ];then
elif [ -f $1 ];then
        echo "Usage: $0 code.c"
        echo "or e.g. head code.c|$0"
        exit 1
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()

share|improve this answer

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


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

Another thing would be to just use nano:

nano testfile

enter image description here

share|improve this answer

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

    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>
share|improve this answer

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.

share|improve this answer
I know. The thing is that openeing and closing VIM is not quick. I want to instantly view a file and then continue on the command line. That's why I found pygmentize such a good one. Instant result, no quiting a program or anything.. :) –  kramer65 Jan 16 '14 at 11:10
@kramer65 OK of course it's your preference, but you don't need to "open" vim or view, you just provide it with the file: view, so no difference there with cat. Sure you need to close it, but really, :q<Return> or ZZ are not difficult tasks. –  a different ben Jan 17 '14 at 1:46

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

share|improve this answer

One can check out ccat.

It adds syntax highlight to output files.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.