Cir*_*郝海东 57
diff --color
选项已添加到 GNU diffutils 3.4 (2016-08-08)
这是diff
大多数发行版的默认实现,很快就会得到它。
Ubuntu 18.04 有diffutils
3.6,因此有它。
在 3.5 上,它看起来像这样:
测试:
diff --color -u \
<(seq 6 | sed 's/$/ a/') \
<(seq 8 | grep -Ev '^(2|3)$' | sed 's/$/ a/')
Run Code Online (Sandbox Code Playgroud)
显然在提交 c0fa19fe92da71404f809aafb5f51cfd99b1bee2(2015 年 3 月)中添加。
词级差异
喜欢diff-highlight
。似乎不可能,功能请求:https : //lists.gnu.org/archive/html/diffutils-devel/2017-01/msg00001.html
相关主题:
ydiff
这样做,见下文。
ydiff
并排字级差异
https://github.com/ymattw/ydiff
这是涅槃吗?
python3 -m pip install --user ydiff
diff -u a b | ydiff -s
Run Code Online (Sandbox Code Playgroud)
结果:
如果线条太窄(默认为 80 列),请使用以下内容进行筛选:
diff -u a b | ydiff -w 0 -s
Run Code Online (Sandbox Code Playgroud)
测试文件内容:
一种
1
2
3
4
5 the original line the original line the original line the original line
6
7
8
9
10
11
12
13
14
15 the original line the original line the original line the original line
16
17
18
19
20
Run Code Online (Sandbox Code Playgroud)
乙
1
2
3
4
5 the original line teh original line the original line the original line
6
7
8
9
10
11
12
13
14
15 the original line the original line the original line the origlnal line
16
17
18
19
20
Run Code Online (Sandbox Code Playgroud)
ydiff
Git集成
ydiff
无需任何配置即可与 Git 集成。
在 git 存储库中git diff
,您可以执行以下操作,而不是:
ydiff -s
Run Code Online (Sandbox Code Playgroud)
而不是git log
:
ydiff -ls
Run Code Online (Sandbox Code Playgroud)
另见:https : //stackoverflow.com/questions/7669963/how-can-i-get-a-side-by-side-diff-when-i-do-git-diff/14649328#14649328
在 Ubuntu 16.04、git 2.18.0、ydiff 1.1 上测试。
Mic*_*mer 24
如果您可以访问 GNU diff
,则可以使用其--X-group-format
选项来获得该效果,而无需任何其他工具:
diff --old-group-format=$'\e[0;31m%<\e[0m' \
--new-group-format=$'\e[0;31m%>\e[0m' \
--unchanged-group-format=$'\e[0;32m%=\e[0m' \
file1 file2
Run Code Online (Sandbox Code Playgroud)
它使用ANSI 颜色转义码来获得红色和绿色,并在 shell 中引用 ANSI-C来访问\e
转义符。
--old-group-format
和--new-group-format
识别非匹配线和用红和色复位码之间插入它们%<
和%>
,而--unchanged-group-format
插入件匹配绿色和复位码之间的线。
您也可以使用--old-line-format
(etc),代价是每行都有多余的颜色转义:--old-line-format=$'\e[0;31m%L\e[0m'
.
小智 11
尝试 colordiff file1 file2
colordiff 在 Linux/BSD 发行版中的可用性
那些运行 Debian 或 Ubuntu(或它们的任何衍生产品)的人可能只需要使用“apt-get install colordiff”来下载和安装;colordiff 还打包用于许多其他 Linux、UNIX 和 BSD 发行版和操作系统。
彩色、字级 diff
输出
您可以使用以下脚本和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
Run Code Online (Sandbox Code Playgroud)
(感谢@retracile 的答案突出显示sed
)
小智 5
如果你安装了 vim,你可以这样做 diff file1 file2 | vim -
Vim 会识别 diff 格式并给它适当的着色。最后的破折号是让 vim 接受来自 diff 命令的输入。