Syntax highlighting/colorizing cat

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

    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 (~/.zshrc, etc..):

    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.

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

    On OSX simply do brew install ccat.

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

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

    just use vim and this vimrc file.

    oneliner:

    vim -c '1' -c 'set cmdheight=1' -c 'set readonly' -c 'set nomodifiable' -c 'syntax enable' -c 'set guioptions=aiMr' -c 'nmap q :q!<CR>' -c 'nmap <Up> <C-Y>' -c 'nmap <Down> <C-E>' -c 'nmap ^V <C-F><C-G>' "$@" 
    

    nano -v may also be an alternative.

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

    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

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

    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

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

    Place in your ~/.bashrc

    function ccat() { docker run -it -v "$(pwd)":/workdir -w /workdir whalebrew/pygmentize $1; }
    

    then

    ccat filename
    

    Whalebrew creates aliases for Docker images so you can run them as if they were native commands. It's like Homebrew, but with Docker images.

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