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'
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'
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
I'm attaching shots for both down below for a good comparison in highlightings
Here is pygmentize
in action:
and this is highlight
:
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.
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>"
.
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).
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.
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.
bat
brew install bat
apt install bat
choco install bat
cargo install --locked bat
(requires cargo
, the rust build tools)see steps for more OS's at https://github.com/sharkdp/bat#on-ubuntu-using-apt
ccat
/usr/local/bin
.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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With