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.
On OSX simply do
brew install ccat
.https://github.com/jingweno/ccat. Like cat but displays content with syntax highlighting. Built in Go.
source-highlight
Maybe it's possible to find interesting
source-highlight
released under GNU: a package different fromhighlight
.Excerpt from
apt-cache show source-highlight
:I did some alias (Cat and PCat, see below) and this is their output
You can install on Debian based with
and add it as alias e.g. in your
.bash_aliases
with something like the line below.Or you can do a similar alias (without the
-i
at the end to have the possibility to pipe in)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:I'd recommend
pygmentize
from the python packagepython-pygments
. You may want to define the following handy alias (unless you useccat
from the ccrypt package).And if you want line numbers:
From late April 2018 onwards:
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:
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.
bat precisely does that and can be aliased to cat
alias cat='bat'
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.
That's the new best solution I've seen for that problem, thanks Beni.