如何在命令行上为diff着色?

本文翻译自:How to colorize diff on the command line?

When I have a diff, how can I colorize it so that it looks good? 当我有一个差异时,如何给它上色以使其看起来不错? I want it for the command line, so please no GUI solutions. 我希望在命令行使用它,因此请不要使用GUI解决方案。


#1楼

参考:https://stackoom.com/question/avQo/如何在命令行上为diff着色


#2楼

And for those occasions when a yum install colordiff or an apt-get install colordiff is not an option due to some insane constraint beyond your immediate control, or you're just feeling crazy , you can re-invent the wheel with a line of sed: 对于那些由于无法立即控制的疯狂限制而无法执行yum install colordiffapt-get install colordiff情况, 或者您只是感到疯狂 ,可以用sed线重新发明轮子:

sed 's/^-/\x1b[41m-/;s/^+/\x1b[42m+/;s/^@/\x1b[34m@/;s/$/\x1b[0m/'

Throw that in a shell script and pipe unified diff output through it. 将其扔到一个shell脚本中,并通过它通过管道传递统一的diff输出。

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 space 2 changes more readily apparent than colordiff can. 1它将使尾随空间2的变化比colordiff更加容易显现。


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. 1顺便说一句,突出显示文件名与修改后的行相同的原因是,要正确区分文件名和修改后的行需要正确地解析diff格式,而使用正则表达式则无法解决。 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. 2但不要尾随制表符。 Apparently tabs don't get their background set, at least in my xterm. 显然,至少在我的xterm中,选项卡没有设置背景。 It does make tab vs space changes stand out a bit though. 它确实使制表符与空间的更改脱颖而出。


#3楼

You can change the subversion config to use colordiff 您可以更改subversion配置以使用colordiff

~/.subversion/config.diff 〜/ .subversion / config.diff

 ### Set diff-cmd to the absolute path of your 'diff' program.
 ###   This will override the compile-time default, which is to use
 ###   Subversion's internal diff implementation.
-# diff-cmd = diff_program (diff, gdiff, etc.)
+diff-cmd = colordiff

via: https://gist.github.com/westonruter/846524 通过: https : //gist.github.com/westonruter/846524


#4楼

Actually there seems to be yet another option (which I only noticed recently, when running into the problem described above): 实际上,似乎还有另一种选择(当遇到上述问题时,我最近才注意到):

git diff --no-index <file1> <file2>
# output to console instead of opening a pager
git --no-pager diff --no-index <file1> <file2>

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. 如果周围有Git(无论如何您可能已经在使用它),那么即使文件本身不受版本控制,也可以将其用于比较。 If not enabled for you by default, then enabling color support here seems to be considerably easier than some of the previously mentioned workarounds. 如果默认情况下未为您启用,那么在这里启用颜色支持似乎比前面提到的某些解决方法容易得多。


#5楼

Coloured, word-level diff ouput 彩色字级 diff输出

Here's what you can do with the the below script and diff-highlight : 这是使用以下脚本和diff-highlight可以执行的操作:

彩色差异截图

#!/bin/sh -eu

# Use diff-highlight to show word-level differences

diff -U3 --minimal "$@" |
  sed 's/^-/\x1b[1;31m-/;s/^+/\x1b[1;32m+/;s/^@/\x1b[1;34m@/;s/$/\x1b[0m/' |
  diff-highlight

(Credit to @retracile's answer for the sed highlighting) (对sed突出显示使用@retracile的答案


#6楼

Since wdiff accepts args specifying the string at the beginning and end of both insertions and deletions, you can use ANSI color sequences as those strings: 由于wdiff接受在插入和删除操作的开头和结尾都指定字符串的args,因此可以将ANSI颜色序列用作这些字符串:

wdiff -n -w $'\033[30;41m' -x $'\033[0m' -y $'\033[30;42m' -z $'\033[0m' file1 file2

For example, this is the output of comparing two CSV files: 例如,这是比较两个CSV文件的输出:

CSV文件的差异输出

Example from https://www.gnu.org/software/wdiff/manual/html_node/wdiff-Examples.html 来自https://www.gnu.org/software/wdiff/manual/html_node/wdiff-Examples.html的示例

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章