'Syntax highlighting/colorizing cat
Is there a method to colorize the output of cat, the way grep does.
For grep, in most consoles it displays a colored output highlighting the searched keywords. Otherwise, you can force it by calling grep --color
Is there a generic way to color the output of any program according to your personal choice.
From what I understand, the program itself is not responsible for the colors. It is the shell.
I am using the default shell in FreeBSD 5.2.1 which looks like it has never seen colors since epoch.
Solution 1:[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'

And if you want line numbers:
alias ccat='pygmentize -g -O style=colorful,linenos=1'
Solution 2:[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:

and this is highlight:

Solution 3:[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:[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>".
Solution 5:[5]
The tool you're looking for is probably supercat (here's a quick introduction published by Linux Journal).
I realize that this answer is late, and that it doesn't fully meet the OP requirements. So I'm adding it just for reference (it could be useful for other people looking for how to colorize text file output).
Solution 6:[6]
There are colorized versions of cat (their names are hard to google, unless you append pager and github or cat replacement).
- bat [rust] https://github.com/sharkdp/bat (actively maintained)
- ccat [golang] https://github.com/jingweno/ccat/
Both bat and ccat are native binaries and are almost as fast as /bin/cat unlike Python-based solutions, such as pygmentize.
installing bat
- MacOS:
brew install bat - Linux:
apt install bat - Windows:
choco install bat - Binaries: https://github.com/sharkdp/bat/releases
- Build from source:
cargo install --locked bat(requirescargo, the rust build tools)
see steps for more OS's at https://github.com/sharkdp/bat#installation
Installing ccat
- get the binaries for Linux/Windows/macOS and copy ccat for example to
/usr/local/bin. - Mac:
brew install ccat
If there is no binary for your platform (e.g. raspberry pi, etc) then you can install from source (requires the golang environment):
go get -u github.com/jingweno/ccat
# NOTE: as of Go 1.18 instead of 'go get xyz' use 'go install xyz', e.g.
go install github.com/jingweno/ccat@latest
Aliasing to cat
The ootb configuration of bat shows line numbers and does paging which I didn't need so I aliased it to disable the feature I didn't want:
Add in your ~/.bashrc (~/.zshrc, etc..):
alias cat="bat --paging=never -pp --style='plain' --theme=TwoDark $*"
For ccat:
alias cat="ccat $*"
In cases when you need the plain cat you can still invoke it by the absolute path:
/bin/cat /etc/hosts
Solution 7:[7]
The best way and the easiest way to do it if you have vim in your machine is to use vimcat which comes with vimpager program.
- Install vimpage with
git clone git://github.com/rkitover/vimpager cd vimpager sudo make install Run vimcat:
vimcat index.html
Solution 8:[8]
source-highlight
Maybe it's possible to find interesting source-highlight released under GNU: a package different from highlight.
Excerpt from apt-cache show source-highlight:
Description-en: convert source code to syntax highlighted document.
This program, given a source file, produces a document with syntax highlighting.
It supports syntax highlighting for over 100 file formats ...
For output, the following formats are supported: HTML, XHTML, LaTeX, Texinfo, ANSI color escape sequences, and DocBook
I did some alias (Cat and PCat, see below) and this is their output

You can install on Debian based with
sudo apt-get install source-highlight
and add it as alias e.g. in your .bash_aliases with something like the line below.
alias Cat='source-highlight --out-format=esc -o STDOUT -i'
Cat myfile.c # or myfile.xml ...
Or you can do a similar alias (without the -iat the end to have the possibility to pipe in)
alias PCat='source-highlight --out-format=esc -o STDOUT '
tail myfile.sh | PCat # Note the absence of the `-i`
Among the options that it's possible to read from man source-highlight the -s underlines that is possible to select, or force, the highlighting by command line or to leave to the program this duty:
-s, --src-lang=STRING source language (use --lang-list to get the complete list). If not specified, the source language will be guessed from the file extension.
--lang-list list all the supported language and associated language definition file
Solution 9:[9]
bat precisely does that and can be aliased to cat alias cat='bat'
Solution 10:[10]
From what I understand, the binary itself is not responsible for the colors. It is the shell.
That't not correct. Terminal just interprets the color codes that is output to the terminal. Depending on its capability it can ignore certain formatting/coloring codes.
From man page it does not seem cat supports coloring its output. Even if it were to support coloring like grep what should it color in the text file? Syntax highlighting required knowledge of underlying language which is not in the scope of simple utility like cat.
You can try more powerful editors like vim,emacs, gedit etc on unix platform if seeing the code highlighted is your goal.
Solution 11:[11]
On OSX simply do brew install ccat.
https://github.com/jingweno/ccat. Like cat but displays content with syntax highlighting. Built in Go.
Solution 12:[12]
Old question, just answering for the record to provide the solution I ended up using. Perhaps this is a bit hacky (probably not the original intent of the parameter), but:
alias cgrep='grep -C 9000'
cat whatever | cgrep 'snozzberries'
..grep -C N will provide N lines of context above and below the found item. If it's larger than the input, the whole input is included. In this case, we just make sure it's larger than any typical terminal output we'll want to look at manually, which is generally what you're looking to do if highlighting.
EDIT : However, this solution suggested below by Beni Cherniavsky-Paskin is superior -- it matches (and highlights) either the word you're looking for or the beginning of the line (not highlightable). The net result is exactly what you want.
cat whatever | egrep 'snozzberries|$'
That's the new best solution I've seen for that problem, thanks Beni.
Solution 13:[13]
In this question https://superuser.com/questions/602294/is-there-colorizer-utility-that-can-take-command-output-and-colorize-it-accordin grcat/grc tool was recommended as alternative to supercat.
Man of grc and of grcat; it is part of grc package (sources):
grc - frontend for generic colouriser grcat(1)
grcat - read from standard input, colourise it and write to standard output
Solution 14:[14]
I have written the small script to perform the colourization using pygmentize.
colorize_via_pygmentize() {
if [ ! -x "$(which pygmentize)" ]; then
echo "package \'Pygments\' is not installed!"
return -1
fi
if [ $# -eq 0 ]; then
pygmentize -g $@
fi
for FNAME in $@
do
filename=$(basename "$FNAME")
lexer=`pygmentize -N \"$filename\"`
if [ "Z$lexer" != "Ztext" ]; then
pygmentize -l $lexer "$FNAME"
else
pygmentize -g "$FNAME"
fi
done
}
And then make an alias to script. alias cat=colorize_via_pygmentize. Also dont forget to save this in ~/.bashrc.
Solution 15:[15]
just use vim and this vimrc file.
oneliner:
vim -c '1' -c 'set cmdheight=1' -c 'set readonly' -c 'set nomodifiable' -c 'syntax enable' -c 'set guioptions=aiMr' -c 'nmap q :q!<CR>' -c 'nmap <Up> <C-Y>' -c 'nmap <Down> <C-E>' -c 'nmap ^V <C-F><C-G>' "$@"
nano -v may also be an alternative.
Solution 16:[16]
Place in your ~/.bashrc
function ccat() { docker run -it -v "$(pwd)":/workdir -w /workdir whalebrew/pygmentize $1; }
then
ccat filename
Whalebrew creates aliases for Docker images so you can run them as if they were native commands. It's like Homebrew, but with Docker images.
Solution 17:[17]
If you just want a one liner to set cat output to a given color, you can append
alias cat="echo -en 'code' | cat - "
to your ~/.$(basename $SHELL)rc
Here is a gist with color codes: https://gist.github.com/chrisopedia/8754917
I like '\e[1;93m', which is high intensity yellow. It looks like this: 
Solution 18:[18]
This question is exceedingly old, but I stumbled on it anyway. For completeness' sake, the question asked "is there a way to get cat to colorize its output?". Yes, for ansi-encoded outputs, you can add these exports to your .bashrc:
# colorful less output
export LESS_TERMCAP_mb=$'\e[1;32m'
export LESS_TERMCAP_md=$'\e[1;32m'
export LESS_TERMCAP_me=$'\e[0m'
export LESS_TERMCAP_se=$'\e[0m'
export LESS_TERMCAP_so=$'\e[01;33m'
export LESS_TERMCAP_ue=$'\e[0m'
export LESS_TERMCAP_us=$'\e[1;4;31m'
This will colorize the output of ansi-encoded text, like terraform plan:

This is not, however, the same thing as bat, which can do better parsing of json, shows line numbers, and is generally a better user experience.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
