linux cat 颜色,`cat`可以使用颜色代码标记显示文件吗?
发布日期:2021-06-24 15:07:03 浏览次数:2 分类:技术文章

本文共 2905 字,大约阅读时间需要 9 分钟。

问题描述

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

cat能做这样的事吗?

最佳解决方案

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

sudo apt-get install python-pygments

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

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

它用于

pygmentize -g

次佳解决方案

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

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

然后去吧:

f57474af8261f852840003eeab5d63ef.png

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

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

chmod +x ~/.bash_aliases

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

if [ -f ~/.bash_aliases ]; then

. ~/.bash_aliases

fi

另一件事就是使用nano:

nano testfile

18c39d27c70f7ef81cd465bd91dc4eac.png

第四种方案

man view或man vim

基本用法:view

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

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

vim具有用view或vim -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

它也会与颜色连接,

catc ...

第六种方案

可以查看ccat。

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

参考资料

转载地址:https://blog.csdn.net/weixin_33554514/article/details/116966243 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:电子科技大学c语言作业提交1,2016年电子科技大学计算机科学与技术程序设计(C语言)复试笔试仿真模拟题...
下一篇:linux+arm中怎样把文件放到mini2440开发板中,最简单的ARM裸机程序,帮你理解程序的运行启动(mini2440开发板) - TQW432......

发表评论

最新留言

能坚持,总会有不一样的收获!
[***.219.124.196]2024年04月11日 18时22分23秒