是否有一种方法为cat的输出着色,就像grep那样。

对于grep,在大多数控制台中,它会显示一个彩色输出,突出显示搜索的关键字。否则,你可以通过调用grep——color来强制执行 是否有一种通用的方法来根据您的个人选择为任何程序的输出着色。

据我所知,程序本身并不负责颜色。它是壳。

我正在使用FreeBSD 5.2.1中的默认shell,它看起来像从epoch以来从未见过颜色。


当前回答

我已经写了一个小脚本来执行使用pygmentze着色。

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
}

然后为脚本创建一个别名。别名猫= colorize_via_pygmentize。另外,不要忘记将其保存在~/.bashrc中。

其他回答

我推荐使用python包python- pyptions中的pygmentize。您可能需要定义以下方便的别名(除非您使用ccrypt包中的ccat)。

alias ccat='pygmentize -g'

如果你想要行号

alias ccat='pygmentize -g -O style=colorful,linenos=1'

向~/添加上述命令中的一个。bash_别名永久生效

在这个问题中,https://superuser.com/questions/602294/is-there-colorizer-utility-that-can-take-command-output-and-colorize-it-accordin grcat/grc工具被推荐作为超级猫的替代品。

人是有真才实学的;它是GRC包的一部分(来源):

GRC -通用着色剂grcat的前端(1) Grcat -从标准输入读取,着色并写入标准输出

Vimcat是单文件(shell脚本),工作良好:

http://www.vim.org/scripts/script.php?script_id=4325

最近一次更新是2013年12月。提示:你可以通过vimcat -c "set ft=<type>"强制文件类型识别。

Bat正是这样做的,可以别名为cat别名cat=' Bat '

在OSX上简单地执行brew install ccat。

https://github.com/jingweno/ccat。与cat类似,但显示内容时使用语法高亮显示。内置围棋。