当前位置: 首页>>技术问答>>正文


`cat`可以使用颜色代码标记显示文件吗?

, ,

问题描述

有时我很快就想从命令行查看文件的内容。为此,我当然使用cat,但它通常是PythonJava或简单HTML中的源文件。对于这些文件,如果cat可以为文件提供一些颜色标记,那将会很方便。

cat能做这样的事吗?

最佳解决方案

cat无法执行此操作。但是,也许pygments可以帮助你。它是一个python脚本,可以通过apt-get安装

sudo apt-get install python-pygments

或通过easy_install轻松下载和安装。

它支持许多源代码languages以及标记语言

它用于

pygmentize -g <filename>

次佳解决方案

不从自身cat,但你可以使用类似source highlitesupercathighlight

Source-highlight

This program, given a source file, produces a document with syntax highlighting. It also provides a C++ highlight library (new) (since version 3.0).

Source-highlight reads source language specifications dynamically, thus it can be easily extended (without recompiling the sources) for handling new languages. It also reads output format specifications dynamically, and thus it can be easily extended (without recompiling the sources) for handling new output formats. The syntax for these specifications is quite easy (take a look at the manual).

The manual about installation:

See the file INSTALL for detailed building and installation instructions; anyway if you’re used to compiling Linux software that comes with sources you may simply follow the usual procedure, i.e., untar the file you downloaded in a directory and then:

 cd <source code main directory>
 ./configure
 make
 make install

Supercat

This is Supercat’s homepage. Supercat is a program that colorizes text based on matching regular expressions/strings/characters. Supercat supports html output as well as standard ASCII text. Unlike some text-colorizing programs that exist, Supercat does not require you to have to be a programmer to make colorization rules.

If you have written a supercat config file for a standard file type please do not hesitate to contact me at “bug-spc (at) nosredna (dot) net” for possible inclusion in the supercat distribution.

或者使用函数(source):

#!/bin/bash#!/bin/bash
if [ ! -t 0 ];then
        file=/dev/stdin
elif [ -f $1 ];then
        file=$1
else
        echo "Usage: $0 code.c"
        echo "or e.g. head code.c|$0"
        exit 1
fi
pygmentize -f terminal -g $file

需要:Pygments(sudo apt-get install python-pygments python3-pygments)将它作为函数添加到bash .functions并命名为 color()

第三种解决方案

从这个答案here开始,您可以使用python-pygments软件包来突出显示内容。先做:

sudo apt-get install python-pygments python3-pygments

然后:

pygmentize -g FILENAME

然后去吧:

colors,cat,ubuntu

您也可以将其设置为别名,就像我链接的答案一样 – 基本上,运行这个:

echo "alias catc='pygmentize -g'" >> ~/.bash_aliases 
chmod +x ~/.bash_aliases

关闭终端,再次打开它,catc命令现在应该工作 – 如果没有,请确保这些行在.bashrc文件中,并且取消注释:

if [ -f ~/.bash_aliases ]; then
. ~/.bash_aliases
fi

另一件事就是使用nano

nano testfile

colors,cat,ubuntu

第四种方案

man viewman vim

基本用法:view <filename>

退出::q<Return>(如果使用vim则首先添加<Esc>)或ZZ(大写z两次)。

程序员的文本编辑器vim已经拥有了您所需要的一切,并且可能已经是您系统的一部分。

vim具有用viewvim -R活化的read-only模式。如果您只想查看marked-up文件,那就足够了。

易于使用,可导航,随处可用。无需安装新软件或编写bash脚本。

第五种方案

cat不能单独生成语法高亮显示。您仍然可以使用python-pygments执行以下操作。首先从终端安装,因为,

sudo apt-get install python-pygments

现在复制~/.bashrc下面的功能。它会给你你想要的东西,而且它将保留cat的属性,否则没有必要使用cat

catc(){
    cat "$@" > /tmp/.tmp
    pygmentize -g /tmp/.tmp
    rm /tmp/.tmp
}

来源~/.bashrc为,

. ~/.bashrc

它会给出彩色输出,

catc <filename>

它也会与颜色连接,

catc <file1> <file2> ... <filen>

第六种方案

可以查看ccat

它为输出文件添加了语法高亮。

参考资料

本文由Ubuntu问答整理, 博文地址: https://ubuntuqa.com/article/2179.html,未经允许,请勿转载。