Syntax highlighting/colorizing cat

前端 未结 18 1933
不思量自难忘°
不思量自难忘° 2020-12-02 03:39

Is there a method to colorize the output of cat, the way grep does.

For grep, in most consoles it displays a colored output hi

18条回答
  •  被撕碎了的回忆
    2020-12-02 04:21

    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.

提交回复
热议问题