如何在命令行上对差异进行着色
当我有差异时,如何对其进行着色以使其看起来不错?
我想要它用于命令行,所以请不要使用 GUI 解决方案。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
当我有差异时,如何对其进行着色以使其看起来不错?
我想要它用于命令行,所以请不要使用 GUI 解决方案。
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(17)
diff
的手册页没有建议从其内部进行着色的解决方案。请考虑使用colordiff
。它是diff
的包装器,产生与 diff 相同的输出,不同之处在于它使用彩色语法突出显示来增强输出以提高可读性:或者只是:
安装:
sudo apt-get安装 colordiff
brew install colordiff
或port install colordiff
Man pages for
diff
suggest no solution for colorization from within itself. Please consider usingcolordiff
. It's a wrapper arounddiff
that produces the same output as diff, except that it augments the output using colored syntax highlighting to increase readability:or just:
Installation:
sudo apt-get install colordiff
brew install colordiff
orport install colordiff
使用 Vim:
或者更好的是,VimDiff (或
vim -d
,输入时间较短)将并排显示两个、三个或四个文件之间的差异 -边。示例:
Use Vim:
Or better still, VimDiff (or
vim -d
, which is shorter to type) will show differences between two, three or four files side-by-side.Examples:
实际上似乎还有另一个选择(我最近才注意到,当遇到上述问题时):
如果你有 Git(你可能已经在使用它),那么你将能够使用它进行比较,即使文件本身不受版本控制。如果默认情况下未启用,那么在此处启用颜色支持似乎比前面提到的一些解决方法要容易得多。
Actually there seems to be yet another option (which I only noticed recently, when running into the problem described above):
If you have Git around (which you already might be using anyway), then you will be able to use it for comparison, even if the files themselves are not under version control. If not enabled for you by default, then enabling color support here seems to be considerably easier than some of the previously mentioned workarounds.
diff --color
选项(于 2016 年 8 月 8 日添加到 GNU diffutils 3.4)这是大多数发行版上的默认
diff
实现,它很快就会得到它。Ubuntu 18.04 (Bionic Beaver) 有
diffutils< /code> 3.6,因此拥有它。
在 3.5 上,它看起来像这样:
测试:
显然已在提交 c0fa19fe92da71404f809aafb5f51cfd99b1bee2 中添加(3 月2015)。
字级差异
类似于
diff-highlight
。看起来不可能,但有一个功能请求: https://lists.gnu.org/archive/html/diffutils-devel/2017-01/msg00001.html相关问题:
ydiff
确实可以。见下文。ydiff
并排单词级别差异https:// github.com/ymattw/ydiff
这是涅槃吗?
结果:
如果行太窄(默认 80 列),请使用以下内容适合屏幕:
测试文件的内容:
a
b
ydiff
Git 集成ydiff
与 Git 集成,无需任何配置。从 Git 存储库内部,您可以执行以下操作,而不是
git diff
:并代替
git log
:另请参阅:如何获得当我执行“git diff”时并排比较?
在 Ubuntu 16.04 (Xenial Xerus)、Git 2.18.0 和 ydiff 1.1.
diff --color
option (added to GNU diffutils 3.4 in 2016-08-08)This is the default
diff
implementation on most distributions, which will soon be getting it.Ubuntu 18.04 (Bionic Beaver) has
diffutils
3.6 and therefore has it.On 3.5 it looks like this:
Tested with:
Apparently added in commit c0fa19fe92da71404f809aafb5f51cfd99b1bee2 (Mar 2015).
Word-level diff
Like
diff-highlight
. It is not possible it seems, but there is a feature request: https://lists.gnu.org/archive/html/diffutils-devel/2017-01/msg00001.htmlRelated questions:
ydiff
does it though. See below.ydiff
side-by-side word level diffhttps://github.com/ymattw/ydiff
Is this nirvana?
Outcome:
If the lines are too narrow (default 80 columns), fit to the screen with:
Contents of the test files:
a
b
ydiff
Git integrationydiff
integrates with Git without any configuration required.From inside a Git repository, instead of
git diff
, you can do just:and instead of
git log
:See also: How can I get a side-by-side diff when I do "git diff"?
Tested on Ubuntu 16.04 (Xenial Xerus), Git 2.18.0, and ydiff 1.1.
对于那些由于某些超出您直接控制范围的疯狂限制而无法使用
yum install colordiff
或apt-get install colordiff
的情况,或者您'如果你只是觉得疯狂,你可以用一行 sed 重新发明轮子:将其放入 shell 脚本中并通过它传输统一差异输出。
它使大块标记变为蓝色,并分别以绿色和红色背景突出显示新/旧文件名以及添加/删除的行。1 并且会使尾随空格2 更改更加明显比 colordiff 可以。
1 顺便说一句,突出显示与修改的行相同的文件名的原因是,要正确区分文件名和修改的行需要正确解析 diff 格式,这不是用正则表达式可以解决的问题。突出显示它们在视觉上“足够好”,并且使问题变得微不足道。也就是说,有一些有趣的微妙之处。
2 但不是尾随制表符。显然,选项卡没有设置背景,至少在我的 xterm 中是这样。不过,它确实使制表符与空格的变化有点突出。
And for those occasions when a
yum install colordiff
or anapt-get install colordiff
is not an option due to some insane constraint beyond your immediate control, or you're just feeling crazy, you can reinvent the wheel with a line of sed:Throw that in a shell script and pipe unified diff output through it.
It makes hunk markers blue and highlights new/old filenames and added/removed lines in green and red background, respectively.1 And it will make trailing space2 changes more readily apparent than colordiff can.
1 Incidentally, the reason for highlighting the filenames the same as the modified lines is that to correctly differentiate between the filenames and the modified lines requires properly parsing the diff format, which is not something to tackle with a regex. Highlighting them the same works "well enough" visually and makes the problem trivial. That said, there are some interesting subtleties.
2 But not trailing tabs. Apparently tabs don't get their background set, at least in my xterm. It does make tab vs. space changes stand out a bit though.
彩色, word level
diff
ouput这是您可以使用以下脚本和diff-highlight:
(归功于 @retracile 的答案用于
sed
突出显示)Coloured, word-level
diff
ouputHere's what you can do with the the below script and diff-highlight:
(Credit to @retracile's answer for the
sed
highlighting)您可以更改 Subversion 配置以使用 colordiff:
~/.subversion/config。差异
通过:https://gist.github.com/westonruter/846524
You can change the Subversion configuration to use colordiff:
~/.subversion/config.diff
via: https://gist.github.com/westonruter/846524
我使用
grc
(通用着色器),它允许您对包括diff
在内的许多命令的输出进行着色。它是一个可以包含任何命令的 Python 脚本。因此,您可以调用
grc diff file1 file2
来查看彩色输出,而不是调用diff file1 file2
。我将diff
别名为grc diff
以使其更容易。I use
grc
(Generic Colouriser), which allows you to colour the output of a number of commands includingdiff
.It is a Python script which can be wrapped around any command. So instead of invoking
diff file1 file2
, you would invokegrc diff file1 file2
to see colourised output. I have aliaseddiff
togrc diff
to make it easier.这是另一个解决方案,它调用
sed
插入适当的 ANSI 转义序列以显示颜色+
、-
和@< /code> 线分别为红色、绿色和青色。
与此问题的其他解决方案不同,此解决方案没有明确说明 ANSI 转义序列。相反,它调用 tput setaf 和 tput sgr0 命令来生成 ANSI 转义序列,以分别设置适当的颜色和重置终端属性。
要查看
tput setaf
的每个参数的可用颜色,请使用以下命令:输出如下:
这是
tput setaf<的证据/代码> 和
tput sgr0
命令生成适当的 ANSI 转义序列:Here is another solution that invokes
sed
to insert the appropriate ANSI escape sequences for colors to show the+
,-
, and@
lines in red, green, and cyan, respectively.Unlike the other solutions to this question, this solution does not spell out the ANSI escape sequences explicitly. Instead, it invokes the
tput setaf
andtput sgr0
commands to generate the ANSI escape sequences to set an appropriate color and reset terminal attributes, respectively.To see the available colors for each argument to
tput setaf
, use this command:Here is how the output looks:
Here is the evidence that the
tput setaf
andtput sgr0
commands generate the appropriate ANSI escape sequences:由于
wdiff
接受在插入和删除的开头和结尾指定字符串的参数,因此您可以使用 ANSI 颜色序列 作为这些字符串:例如,这是比较两个 CSV 文件的输出:
示例2.2 wdiff 的实际示例用法。
Since
wdiff
accepts arguments specifying the string at the beginning and end of both insertions and deletions, you can use ANSI color sequences as those strings:For example, this is the output of comparing two CSV files:
Example from 2.2 Actual examples of wdiff usage.
到目前为止,还没有人提到 delta。它支持带有语法突出显示的语法彩色差异视图。
No one has mentioned delta so far. It supports syntax colored diff view with syntax highlighting.
字符级颜色差异:
安装 ccdiff
Character-level color diff:
Install ccdiff
我建议您尝试 diff-so-fancy 。我在工作期间使用它,目前看起来确实很棒。它包含许多选项,并且可以轻松按照您想要的方式配置差异。
您可以通过以下方式安装它:
或在 Mac 上:
然后,您可以像这样突出显示您的差异:
I would suggest you to give diff-so-fancy a try. I use it during my work and it sure seems great as of now. It comes packed with many options and it's really easy to configure your diffs the way you want.
You can install it by:
or on Mac:
Afterwards, you can highlight your diffs like this:
为我工作
works for me
使用 bat 命令:
With the bat command:
在 Ubuntu 上最新版本的 Git 上,您可以通过以下方式启用差异突出显示:
然后将其添加到您的
.gitconfig
文件中:该脚本可能位于其他发行版的其他位置。您可以使用
locate diff-highlight
来查找位置。On recent versions of Git on Ubuntu, you can enable diff-highlighting with:
And then adding this to your
.gitconfig
file:It's possible the script is located somewhere else in other distributions. You can use
locate diff-highlight
to find out where.我最喜欢的选择是
vdiff;
函数(我忘了从哪里得到它)。它将在 Vim 中并排打开两个窗口,以便清楚地看到查看两个文件之间的差异。
将此脚本放入您的 (.alias) 或 (.zshrc) 中,然后使用
vdiff <文件1> <文件2>。
示例
结果为:
My favorite choice is
vdiff <file1> <file2>
function (I forgot from where I got it).It will open two windows in Vim side-by-side, to see clearly see the difference between the two files.
Put this script in your (.alias) or (.zshrc), and then call it using
vdiff <file1> <file2>.
Example
The results are: