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?
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-getor easily downloaded and installed via easy_install.
It supports lots of source code languages and also markup languages
It is used by
Not from
cat
itself but you can use something like source highlite or supercat or highlightOr with a function (source):
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()As from this answer here, you can use the
python-pygments
packages to highlight stuff. First do:then:
then have a go:
You can also set it as an alias, like in the answer I linked - basically, run this:
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:Another thing would be to just use
nano
:man view
orman vim
Basic usage:
view <filename>
Quit:
:q<Return>
(add an<Esc>
first if usingvim
), orZZ
(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 withview
orvim -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.
One can check out ccat.
It adds syntax highlight to output files.
cat
can not produce syntax highlighting solely. Still you can do this as follows, usingpython-pygments
. First install it from terminal as,Now copy the function below
~/.bashrc
. It will give you what you want moreover it will preserve the properties ofcat
otherwise there is no point of usingcat
Source
~/.bashrc
as,It will give colourized output,
It will concatenate with color as well,
Bat — A cat clone with wings
You might also want to check out
bat
which has the following features:less
cat
when redirectedPreview
Picture taken from the official GitHub
Installation
You can get the latest Debian package here and install it with:
Adapting the version number and architecture.
Other answers cover why
cat
is not able to do it. Though you can do it with less using lesspipe.sh.Rather than installing a third party package, you can simply use
gedit
to quickly view a file with syntax highlighting. For example copy the address bar above and paste into your terminal:You will see this question in HTML with formatted colors:
gedit
navigation keyscat
which doesn't require keystrokes to close.