286

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.

Volker Siegel
  • 3,277
  • 2
  • 24
  • 35
Lelouch Lamperouge
  • 8,171
  • 8
  • 49
  • 60
  • oh no. I don't want to display a binary. I just want to display the output of a binary in a colorized manner. – Lelouch Lamperouge Oct 21 '11 at 15:41
  • I edited to avoid the confusion regarding the term "binary" – Volker Siegel Aug 10 '14 at 16:52
  • 3
    I think that the answer by @buergi matches the question, and this is also supported by the number of upvotes it has. Consider changing the accepted answer. – 0 _ Sep 06 '14 at 03:47
  • The best solution is: `sudo apt-get install lolcat && echo {a..z}{a..z}{a..z} | lolcat` – Ciro Santilli OurBigBook.com Oct 23 '14 at 13:47
  • 1
    TL;DR(Correct me if I am wrong): [highlight](https://stackoverflow.com/a/27501509/6397520) (I use), [pygmentize](https://stackoverflow.com/a/14799752/6397520), [bat](https://stackoverflow.com/a/50220911/6397520), [vimcat](https://stackoverflow.com/a/20826100/6397520), [supercat](https://stackoverflow.com/a/10039345/6397520), [ccat](https://stackoverflow.com/a/30074374/6397520), [source-highlight?](https://stackoverflow.com/a/30126756/6397520). For `less` use: `yourColorCatCommand yourFile | less -R`. – bogec Apr 03 '21 at 10:21
  • As @myfirstAnswer said, "bat" (batcat) is an alternative solution for "code highlighting" instead of `cat`. – slayer Nov 28 '21 at 20:31

20 Answers20

484

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'

Add one of these above commands to ~/.bash_aliases for permanent effect

João Pimentel Ferreira
  • 14,289
  • 10
  • 80
  • 109
buergi
  • 6,039
  • 3
  • 19
  • 15
  • 42
    Another solution is to use the linux [`highlight`](http://linux.die.net/man/1/highlight) command. `alias ccat='highlight -O ansi'` – Evan Purkhiser Oct 24 '13 at 18:33
  • step (1) `easy_install --install-dir=/your/pygmentize/path Pygments`, step (2) `alias ccat /your/pygmentize/path/pygmentize -g` – Daniel Nov 04 '13 at 13:00
  • 123
    This should be the accepted answer IMHO. Adding to that, I aliased it as `alias dog='pygmentize -g'`, because dogs are cooler than cats! – polym Jul 13 '14 at 08:28
  • 9
    You can add less -R to make the code scrollable: `#!/bin/bash \n pygmentize -g $1 | less -R` – Rudolf Real Jan 02 '15 at 15:03
  • 12
    I called my alias `nyancat`. – zneak Aug 20 '15 at 15:39
  • 7
    pygmentize works as expected, but unfortunately seems to be rather slow. :-/ – Frerich Raabe Mar 23 '17 at 11:47
  • How is it possible to connect it to `nano`? – kyb Apr 16 '17 at 22:09
  • BTW, pygmentize also supports monokai style, which makes it exactly what I wanted (i.e. Sublime Text-like highlighting). – Alexander Revo May 19 '17 at 10:29
  • @FrerichRaabe try `highlight` as suggested above - it's noticeably faster in my case. – ttb Mar 02 '18 at 18:57
  • 1
    Also, when using the `highlight` command in `alias` or a function, add the `--force` option to output even when it cannot guess what the filetype is. – Chromium Sep 02 '18 at 04:14
  • Why `-g` ? I couldn't find any information in pygmentize's command line usage document about the option `-g` – 千木郷 Sep 22 '18 at 15:34
  • According to `pygments -h`: `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).` – buergi Sep 23 '18 at 16:10
  • Don't forget to add the `alias` command to `~/.bashrc` if you don't want to repeat it every time you login – João Pimentel Ferreira Aug 02 '20 at 16:24
  • @EvanPurkhiser I just don't like the red javascript strings of `highlight`. I have never seen such JS syntax highlight, putting strings on vivid red. – João Pimentel Ferreira Aug 02 '20 at 16:30
  • I went for it... until I realized there is not a single style in default Pygments that works well on a dark background. All that I tried on Ubuntu output some elements in unreadable dark blue. – Victor Sergienko Dec 11 '20 at 00:46
160

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

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

pjpscriv
  • 866
  • 11
  • 20
Shubham Chaudhary
  • 47,722
  • 9
  • 78
  • 80
  • Thanks! It seems a bit slow, any way to make it faster? – user Jan 27 '16 at 15:57
  • 7
    `highlight` worked great for my needs and installed easily, thanks. If you want to look at files without extensions (e.g. .bash_aliases) then add `--syntax=bash` in addition to `-O ansi` to force it. – jerclarke Mar 26 '16 at 02:50
  • 1
    # pip install Pygments # echo "alias catc='pygmentize -g'" >> "~/.bashrc" # close terminal # open terminal # catc test.js --- and perfect works :-) – Bruno Feb 03 '17 at 06:47
  • Is there a way to make "highlight" guess the input language, much like the `-g` switch does for "pygmentize"? – Frerich Raabe Mar 23 '17 at 11:48
  • 5
    `highlight` is 34x faster than that python program. The power of the classical languages holds strong. – Avindra Goolcharan Nov 04 '17 at 23:15
  • I also feel that `pygmentize` is very slow now a days. I guess most of the time must go in regex matching. – Shubham Chaudhary Nov 08 '17 at 06:37
  • 3
    For `highlight`, you want to add `--force` to the alias, otherwise it will throw an error for file formats that it does not understand, while as `cat` replacement you want it to still show the output without highlighting, which is what `--force` does. All together: `alias cat="highlight -O ansi --force"` – Alexander Klimetschek Dec 04 '17 at 18:38
  • 1
    Thank you so much for replying, I was looking for this kind of tool, awesome! – Sebas Mardini May 17 '18 at 21:54
  • tried `highlight` but it give me some html output not the syntax highlighted content, [Imgur](https://imgur.com/Peio46v) what is the problem? how could I fix it? – Luke Feb 09 '20 at 12:16
  • 2
    @Luke that's why you should add the `-O ansi` the default is to output html :-) – Thomas Feb 20 '20 at 09:09
  • 7
    `-O xterm256` provides more colours than `-O ansi`, if your terminal supports it – naught101 Jun 10 '20 at 09:56
  • I just don't like the red javascript strings of `highlight`. I have never seen such JS syntax highlight, putting strings on vivid red. – João Pimentel Ferreira Aug 02 '20 at 16:29
  • 1
    @Alexander Klimetschek I wouldn't alias cat itself here. This might mess up its usage in pipe commands and scripts. – SO Stinks Dec 18 '21 at 01:15
116

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.

Daniel
  • 11,332
  • 9
  • 44
  • 72
  • 16
    What people forget to mention is that `bat` stays in interactive mode by default. For it to behave more like `cat`, use it with `-pp` argument. An alias like ```alias cat="bat -pp"``` is propably what people are looking for. – Canella Sep 13 '19 at 08:24
  • 2
    I've been using pygmentize for colorizing cat before, but the solution with bat is much better suited for large files (logs of several MB), where pygmentize performance is subpar. – Christian.D Oct 21 '19 at 12:22
  • 1
    Great answer! Together with the -p option, and less paging, this is exactly what I wanted – random_error Jun 04 '21 at 12:49
  • 3
    bat is the coolest thing I've seen this week. This is awesome, thanks! – Max Power Aug 11 '22 at 15:46
  • Yes, this is so much better than pygmentize, thank you ! – Michał Lepczyński Nov 30 '22 at 21:03
  • Just what I needed. On ubuntu while installing using `apt install bat`, the command to use is `batcat`. For removing paging while preserving bat's excellent line formatting, the command is `batcat --paging "never" $file`. – Ajay Singh Mar 19 '23 at 05:23
44

There are colorized versions of cat (their names are hard to google, unless you append pager and github or cat replacement).

Both bat and ccat are native binaries and are almost as fast as /bin/cat unlike Python-based solutions, such as pygmentize.

installing bat

see steps for more OS's at https://github.com/sharkdp/bat#installation

Installing 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 ottb cat you can still invoke the unaliased version by prefixing with a backslash, e.g.

\cat /etc/hosts

or using the absolute path:

/bin/cat /etc/hosts
ccpizza
  • 28,968
  • 18
  • 162
  • 169
  • 1
    What's the proper repo name? apt-get ccat in Ubuntu 16 gives me some sort of encryption program and somewhat hilariously bricked my terminal as it's currently trying to encrypt or decrypt my entire proc directory... – Katastic Voyage Sep 06 '17 at 07:41
  • @KatasticVoyage: my bad: for some reason I assumed it was in the repos, and I forgot how I got it in my ubuntu box. See the updated answer. – ccpizza Sep 06 '17 at 13:14
  • 2
    Currently, it seems like `brew install ccat` is enough, no need to `brew tap` anymore. – Jeff Huijsmans Oct 20 '17 at 13:49
  • Yeah, `ccat` isn't the best name choice as `ccat` is already part of `ccrypt`. – IpsRich Jan 22 '21 at 13:00
  • 1
    I really enjoy how `bat --help` colorizes its own usage information. – Nels Aug 27 '22 at 16:50
  • I also think that `bat` was the easiest to install from its project release. One must only establish an alias which instructs `bat` to not use paging, if one intends to use it as a drop-in replacement for `cat`: `alias cat='bat --paging=never'` – Nels Aug 27 '22 at 17:06
  • I really enjoyed the `bat` application, thanks! – Guilherme Abacherli Oct 05 '22 at 23:28
43

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>".

oHo
  • 51,447
  • 27
  • 165
  • 200
Soergener
  • 703
  • 1
  • 6
  • 7
  • 15
    One thing that is really nice about vimcat (or vimpager) is that it will respect the colorscheme you have defined in your `~/.vimrc` so syntax highlighting will be the same both when wanting to edit (vim) or print out (vimcat). Note that an updated version of vimcat can be found in the [vimpager repositiory](https://github.com/rkitover/vimpager/blob/master/vimcat). – timss Sep 05 '14 at 09:53
  • 1
    [Amaynut's answer](http://stackoverflow.com/a/31971848/938111) is older and similar – oHo Aug 26 '15 at 14:27
  • `vimpager` doesn't use my vim colorscheme. – WhyNotHugo Jan 09 '17 at 15:34
  • 2
    In my quick unscientific test, `vimcat` (1~2 second execution time) is much slower than `pymentize` (~200ms), which is much slower than `highlight` (~20ms), which is much slower than `cat` (~2ms). On the other hand, `vimcat` handles more file types and is more accurate than `hightlight` and `pygmentize`, and it is still faster than first `vim` then `:q`. So it earns my pick for this specific task. – Penghe Geng Aug 20 '18 at 14:55
  • 1
    @olibre This answer seems older than the one from Amaynut. – Franklin Yu Jan 27 '20 at 07:30
34

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

mfriedman
  • 882
  • 1
  • 9
  • 13
  • 1
    This doesn't seem to work so easily, I installed it and used the example config, but that had no colour effect – rubo77 Jan 16 '16 at 21:46
  • Nice, everything else is too slow. This is good. For help with configuration, see https://manpages.debian.org/testing/supercat/spc.1.en.html – okovko Oct 09 '18 at 06:36
  • Additionally for some configs for various languages: https://github.com/tanderson92/supercat/tree/master/spcrc – okovko Oct 09 '18 at 07:23
28

cat with syntax highlighting is simply out of scope. cat is not meant for that. If you just want to have the entire content of some file coloured in some way (with the same colour for the whole file), you can make use of terminal escape sequences to control the color.

Here's a sample script that will choose the colour based on the file type (you can use something like this instead of invoking cat directly):

#!/bin/bash
fileType="$(file "$1" | grep -o 'text')"
if [ "$fileType" == 'text' ]; then
    echo -en "\033[1m"
else
    echo -en "\033[31m"
fi
cat $1
echo -en "\033[0m"

The above (on a terminal that supports those escape sequences) will print any text file as 'bold', and will print any binary file as red. You can use strings instead of cat for printing binary files and you can enhance the logic to make it suit your needs.

pjpscriv
  • 866
  • 11
  • 20
Costi Ciudatu
  • 37,042
  • 7
  • 56
  • 92
  • I was thinking of something like this. But I need something that would do it universally. Not just `cat`/`grep`/`any particular program` – Lelouch Lamperouge Oct 21 '11 at 22:58
  • Then you can simply pass the command to be executed as an argument to the script and replace the hard-coded `cat` invocation with some `$cmd` which is initialized as $1 if there are two arguments or a default if there's only one. Then you simply write: `colorful.sh grep file`. – Costi Ciudatu Nov 09 '12 at 11:18
  • 14
    "cat is not meant for that." Cat isn't meant for writing individual files to stdout either - it's for concatenation. But that still doesn't mean that writing individual files to stdout isn't useful. Not does that mean highlighting them isn't useful. – mikemaccana Oct 09 '17 at 15:36
  • I tried, under cygwin `pygmentize` is not able to color. Any tips? – daparic Sep 07 '18 at 22:17
16

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.

  1. Install vimpage with git clone git://github.com/rkitover/vimpager cd vimpager sudo make install
  2. Run vimcat:

    vimcat index.html

Amaynut
  • 4,091
  • 6
  • 39
  • 44
  • I love the idea, but vimcat is among the slower ones here. It's about as slow as pygmentize. 15x slower than `highlight` and 3x slower than `bat` for a 30-line bash script on my machine. – Ponkadoodle Dec 22 '19 at 20:42
  • `vimcat` is a very nice solution. However, when I use an example (`vimcat afile.md`) I have to press any key to see the result. How can I fix this? – Unix Aug 10 '22 at 20:03
13

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

Screen Example

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

Benjamin Loison
  • 3,782
  • 4
  • 16
  • 33
Hastur
  • 2,470
  • 27
  • 36
  • 1
    If you want to also colorize `less` with `source-highlight`, someone on [GitHub](https://github.com/jrunning/source-highlight-solarized) gives the answer : `export LESSOPEN="| source-highlight -f esc -i %s -o STDOUT"` and `export LESS=" -R "`. Very useful when `pygmentize` is not available and cannot be installed. – Lalylulelo Feb 26 '19 at 10:30
  • @hastur pcat is not working for me. `source-highlight: missing feature: language inference requires input file` – Khurshid Alam Aug 07 '20 at 09:04
  • @KhurshidAlam Maybe it is not one of the supported language. Try _`--lang-list` list all the supported language and associated language definition file_. Check if the language is supported, or force one enough similar... (BTW without your command line it is diffucult to guess what happened). It can even be a bad guess from the )"extension"_. Good Luck. – Hastur Aug 07 '20 at 13:00
9

bat precisely does that and can be aliased to cat alias cat='bat'

Sailendra Pinupolu
  • 1,038
  • 1
  • 10
  • 8
  • bat is realllly nice, written in Rust so very performant and safe. Love the colors and cleanness. I recommend the flag -p as in `bat -p yourFileName` – jasonleonhard Sep 22 '20 at 03:26
7

On OSX simply do brew install ccat.

https://github.com/jingweno/ccat. Like cat but displays content with syntax highlighting. Built in Go.

joost
  • 6,549
  • 2
  • 31
  • 36
  • Nice. Has some language limitations but it does feel a lot faster than pygments. – Gustavo Bezerra Mar 27 '16 at 03:28
  • [ccat](https://aur.archlinux.org/packages/ccat/) can also be found in the AUR repository of Arch Linux. Use yaourt or pacaur to install the package. – Marcs Aug 07 '16 at 14:57
7

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.

Ashwinee K Jha
  • 9,187
  • 2
  • 25
  • 19
  • 3
    “`cat` isn't for printing files with line numbers, it isn't for compressing multiple blank lines, it's not for looking at non-printing ASCII characters, it's for concatenating files.” – from [UNIX Style, or cat -v Considered Harmful](http://harmful.cat-v.org/cat-v). – Scott - Слава Україні Jan 15 '14 at 23:15
3

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.

Benjamin Loison
  • 3,782
  • 4
  • 16
  • 33
Mr. B
  • 2,536
  • 1
  • 26
  • 26
3

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

osgx
  • 90,338
  • 53
  • 357
  • 513
1

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.

Benjamin Loison
  • 3,782
  • 4
  • 16
  • 33
1

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.

eadmaster
  • 1,347
  • 13
  • 23
  • Hi, this seems like the `less` command. What we need is `cat`. BTW: when you have colored `cat` command, you can use `less -R` as in `someColorCatCommand someFile | less -R` – bogec Apr 03 '21 at 10:03
1

I wrote a small Rust-based software to do exactly that. It is named highlighter and can be found in the following repo:

https://codeberg.org/mehrad/highlighter

Basically you just pipe the output of the previous command into it and tell it to color it based on your preference. For example:

locale | highlighter  --blue="=" --red="_U.*" --yellow "NAME" --green="_ME.*"

I suggest creating an alias to it if you think the name is too long. I'd rather go with a longer but descriptive name than a short ambiguous one.

Mehrad Mahmoudian
  • 3,466
  • 32
  • 36
0

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.

javajon
  • 1,660
  • 16
  • 18
-1

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: enter image description here

okovko
  • 1,851
  • 14
  • 27
  • Hi, I don't think this was the OP question. I think the question was not to color all text in one color, but to color in different colors according to specific program language. – bogec Apr 03 '21 at 09:55
  • @myfirstAnswer I don't think so, OP is asking for a way to color like grep, and grep uses one color. The other answers are instead focused on syntax highlighting, which is not what OP asked for. – okovko Apr 03 '21 at 10:22
  • @myfirstAnswer In fact if you read the accepted answer you will see a discussion where OP is looking for a way to set one color for the whole output. But thank you for reading my answer and giving me feedback. – okovko Apr 03 '21 at 10:25
  • @myfirstAnswer It's not really worth your time. It's an old question and the answer has been selected. Good bye. – okovko Apr 03 '21 at 10:28
-1

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: enter image description here

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.

Zach Folwick
  • 893
  • 10
  • 18