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条回答
闹够了就滚
2楼-- · 2019-01-09 22:46

On OSX simply do brew install ccat.

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

查看更多
可以哭但决不认输i
3楼-- · 2019-01-09 22:47

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

查看更多
可以哭但决不认输i
4楼-- · 2019-01-09 22:50

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'
查看更多
孤傲高冷的网名
5楼-- · 2019-01-09 22:51

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.

查看更多
祖国的老花朵
6楼-- · 2019-01-09 22:51

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

查看更多
疯言疯语
7楼-- · 2019-01-09 22:52

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.

查看更多
登录 后发表回答