Syntax highlighting/colorizing cat

2019-01-09 22:29发布

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.

16条回答
Explosion°爆炸
2楼-- · 2019-01-09 23:05

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

查看更多
Fickle 薄情
3楼-- · 2019-01-09 23:06

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

查看更多
唯我独甜
4楼-- · 2019-01-09 23:08

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

查看更多
唯我独甜
5楼-- · 2019-01-09 23:09

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:

sudo pip install pygments
sudo easy_install Pygments #for Mac user

and for highlight package which is easily available on all distributions

sudo apt-get 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

查看更多
登录 后发表回答