Syntax highlighting/colorizing cat

前端 未结 18 1853
不思量自难忘°
不思量自难忘° 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:16

    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.

    0 讨论(0)
  • 2020-12-02 04:17

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

    0 讨论(0)
  • 2020-12-02 04:17

    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

    0 讨论(0)
  • 2020-12-02 04:17

    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.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-02 04:22

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

    0 讨论(0)
提交回复
热议问题