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条回答
The star\"
2楼-- · 2019-01-09 22:54

I have written the small script to perform the colourization using pygmentize.

colorize_via_pygmentize() {
    if [ ! -x "$(which pygmentize)" ]; then
        echo "package \'Pygments\' is not installed!"
        return -1
    fi

    if [ $# -eq 0 ]; then
        pygmentize -g $@
    fi

    for FNAME in $@
    do
        filename=$(basename "$FNAME")
        lexer=`pygmentize -N \"$filename\"`
        if [ "Z$lexer" != "Ztext" ]; then
            pygmentize -l $lexer "$FNAME"
        else
            pygmentize -g "$FNAME"
        fi
    done
}

And then make an alias to script. alias cat=colorize_via_pygmentize. Also dont forget to save this in ~/.bashrc.

查看更多
Rolldiameter
3楼-- · 2019-01-09 22:56

The best way and the easiest way to do it if you have vim in your machine is to use vimcat which comes with vimpager program.

  1. Install vimpage with git clone git://github.com/rkitover/vimpager cd vimpager sudo make install
  2. Run vimcat:

    vimcat index.html

查看更多
甜甜的少女心
4楼-- · 2019-01-09 23:00

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.

查看更多
我命由我不由天
5楼-- · 2019-01-09 23:01

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

查看更多
淡お忘
6楼-- · 2019-01-09 23:02

There is a colorized version of cat - ccat. Get it from https://github.com/jingweno/ccat/.

Installation on Linux/Windows/macOS

It is a single standalone executable so to install it you can unpack the binary version for your OS from https://github.com/jingweno/ccat/releases and copy the ccat binary for example to /usr/local/bin.

If you want to avoid binaries, or if there is no binary for your platform (e.g. raspberry pi, etc), then you can alternately compile from source given that you have a working go development environment (apt install golang on debian-based linuxes or brew install golang on mac):

go get -u github.com/jingweno/ccat

The ccat binary will be created under your $GOPATH/bin.

Installing on Mac via homebrew

brew install ccat

Alias to cat

To replace regular cat with ccat add in ~/.bashrc:

alias cat="ccat $*"
alias cat0="/bin/cat $*" # for cases when you need plain `cat`

ccat is implemented in Go, so it is a native binary which runs much faster than Python-based solutions, such as pygments, the module behind pygmentize; I didn't see any noticeable speed difference between cat and ccat.

查看更多
在下西门庆
7楼-- · 2019-01-09 23:04

From what I understand, the binary itself is not responsible for the colors. It is the shell.

That't not correct. Terminal just interprets the color codes that is output to the terminal. Depending on its capability it can ignore certain formatting/coloring codes.

From man page it does not seem cat supports coloring its output. Even if it were to support coloring like grep what should it color in the text file? Syntax highlighting required knowledge of underlying language which is not in the scope of simple utility like cat.

You can try more powerful editors like vim,emacs, gedit etc on unix platform if seeing the code highlighted is your goal.

查看更多
登录 后发表回答