如何在命令行上对差异进行着色

发布于 2024-12-25 18:27:12 字数 66 浏览 1 评论 0 原文

当我有差异时,如何对其进行着色以使其看起来不错?

我想要它用于命令行,所以请不要使用 GUI 解决方案。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(17

假情假意假温柔 2025-01-01 18:27:12

diff 的手册页没有建议从其内部进行着色的解决方案。请考虑使用colordiff。它是 diff 的包装器,产生与 diff 相同的输出,不同之处在于它使用彩色语法突出显示来增强输出以提高可读性:

diff old new | colordiff

或者只是:

colordiff old new

安装:

  • Ubuntu/Debian:sudo apt-get安装 colordiff
  • OS X:brew install colordiffport install colordiff

Man pages for diff suggest no solution for colorization from within itself. Please consider using colordiff. It's a wrapper around diff that produces the same output as diff, except that it augments the output using colored syntax highlighting to increase readability:

diff old new | colordiff

or just:

colordiff old new

Installation:

  • Ubuntu/Debian: sudo apt-get install colordiff
  • OS X: brew install colordiff or port install colordiff
你曾走过我的故事 2025-01-01 18:27:12

使用 Vim

diff /path/to/a /path/to/b | vim -R -

或者更好的是,VimDiff (或 vim -d,输入时间较短)将并排显示两个、三个或四个文件之间的差异 -边。

示例:

vim -d /path/to/[ab]

vimdiff file1 file2 file3 file4

Use Vim:

diff /path/to/a /path/to/b | vim -R -

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:

vim -d /path/to/[ab]

vimdiff file1 file2 file3 file4
窝囊感情。 2025-01-01 18:27:12

实际上似乎还有另一个选择(我最近才注意到,当遇到上述问题时):

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

如果你有 Git(你可能已经在使用它),那么你将能够使用它进行比较,即使文件本身不受版本控制。如果默认情况下未启用,那么在此处启用颜色支持似乎比前面提到的一些解决方法要容易得多。

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. If not enabled for you by default, then enabling color support here seems to be considerably easier than some of the previously mentioned workarounds.

少女情怀诗 2025-01-01 18:27:12

diff --color 选项(于 2016 年 8 月 8 日添加到 GNU diffutils 3.4)

这是大多数发行版上的默认 diff 实现,它很快就会得到它。

Ubuntu 18.04 (Bionic Beaver) 有 diffutils< /code> 3.6,因此拥有它。

在 3.5 上,它看起来像这样:

在此处输入图像描述

测试:

diff --color -u \
  <(seq 6 | sed 's/$/ a/') \
  <(seq 8 | grep -Ev '^(2|3)

显然已在提交 c0fa19fe92da71404f809aafb5f51cfd99b1bee2 中添加(3 月2015)。

字级差异

类似于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

结果:

Enter此处的图像描述

如果行太窄(默认 80 列),请使用以下内容适合屏幕:

diff -u a b | ydiff -w 0 -s

测试文件的内容:

a

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

b

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

ydiff Git 集成

ydiff 与 Git 集成,无需任何配置。

从 Git 存储库内部,您可以执行以下操作,而不是 git diff

ydiff -s

并代替 git log

ydiff -ls

另请参阅:如何获得当我执行“git diff”时并排比较?

Ubuntu 16.04 (Xenial Xerus)、Git 2.18.0 和 ydiff 1.1.

| sed 's/$/ a/')

显然已在提交 c0fa19fe92da71404f809aafb5f51cfd99b1bee2 中添加(3 月2015)。

字级差异

类似于diff-highlight。看起来不可能,但有一个功能请求: https://lists.gnu.org/archive/html/diffutils-devel/2017-01/msg00001.html

相关问题:

ydiff 确实可以。见下文。

ydiff 并排单词级别差异

https:// github.com/ymattw/ydiff

这是涅槃吗?


结果:

Enter此处的图像描述

如果行太窄(默认 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:

Enter image description here

Tested with:

diff --color -u \
  <(seq 6 | sed 's/$/ a/') \
  <(seq 8 | grep -Ev '^(2|3)

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

Related questions:

ydiff does it though. See below.

ydiff side-by-side word level diff

https://github.com/ymattw/ydiff

Is this nirvana?

python3 -m pip install --user ydiff
diff -u a b | ydiff -s

Outcome:

Enter image description here

If the lines are too narrow (default 80 columns), fit to the screen with:

diff -u a b | ydiff -w 0 -s

Contents of the test files:

a

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

b

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

ydiff Git integration

ydiff integrates with Git without any configuration required.

From inside a Git repository, instead of git diff, you can do just:

ydiff -s

and instead of git log:

ydiff -ls

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.

| sed 's/$/ a/')

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

Related questions:

ydiff does it though. See below.

ydiff side-by-side word level diff

https://github.com/ymattw/ydiff

Is this nirvana?


Outcome:

Enter image description here

If the lines are too narrow (default 80 columns), fit to the screen with:


Contents of the test files:

a


b


ydiff Git integration

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

我要还你自由 2025-01-01 18:27:12

对于那些由于某些超出您直接控制范围的疯狂限制而无法使用 yum install colordiffapt-get install colordiff 的情况,或者您'如果你只是觉得疯狂,你可以用一行 sed 重新发明轮子:

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

将其放入 shell 脚本中并通过它传输统一差异输出。

它使大块标记变为蓝色,并分别以绿色和红色背景突出显示新/旧文件名以及添加/删除的行。1 并且会使尾随空格2 更改更加明显比 colordiff 可以。


1 顺便说一句,突出显示与修改的行相同的文件名的原因是,要正确区分文件名和修改的行需要正确解析 diff 格式,这不是用正则表达式可以解决的问题。突出显示它们在视觉上“足够好”,并且使问题变得微不足道。也就是说,有一些有趣的微妙之处

2 但不是尾随制表符。显然,选项卡没有设置背景,至少在我的 xterm 中是这样。不过,它确实使制表符与空格的变化有点突出。

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 reinvent the wheel with a line of 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.

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.

ペ泪落弦音 2025-01-01 18:27:12

彩色, word level diff ouput

这是您可以使用以下脚本和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

(归功于 @retracile 的答案用于 sed 突出显示)

Coloured, word-level diff ouput

Here's what you can do with the the below script and diff-highlight:

Coloured diff screenshot

#!/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)

小巷里的女流氓 2025-01-01 18:27:12

您可以更改 Subversion 配置以使用 colordiff:

~/.subversion/config。差异

 ### 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

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

You can change the Subversion configuration to use colordiff:

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

兲鉂ぱ嘚淚 2025-01-01 18:27:12

我使用 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 including diff.

It is a Python script which can be wrapped around any command. So instead of invoking diff file1 file2, you would invoke grc diff file1 file2 to see colourised output. I have aliased diff to grc diff to make it easier.

抱猫软卧 2025-01-01 18:27:12

这是另一个解决方案,它调用 sed 插入适当的 ANSI 转义序列以显示颜色 +-@< /code> 线分别为红色、绿色和青色。

diff -u old new | sed "s/^-/$(tput setaf 1)&/; s/^+/$(tput setaf 2)&/; s/^@/$(tput setaf 6)&/; s/$/$(tput sgr0)/"

与此问题的其他解决方案不同,此解决方案没有明确说明 ANSI 转义序列。相反,它调用 tput setaf 和 tput sgr0 命令来生成 ANSI 转义序列,以分别设置适当的颜色和重置终端属性。

要查看 tput setaf 的每个参数的可用颜色,请使用以下命令:

for i in {0..255}; do tput setaf $i; printf %4d $i; done; tput sgr0; echo

输出如下:

在此处输入图像描述

这是tput setaf<的证据/代码> 和tput sgr0 命令生成适当的 ANSI 转义序列:

$ tput setaf 1 | xxd -g1
00000000: 1b 5b 33 31 6d                                   .[31m
$ tput setaf 2 | xxd -g1
00000000: 1b 5b 33 32 6d                                   .[32m
$ tput setaf 6 | xxd -g1
00000000: 1b 5b 33 36 6d                                   .[36m
$ tput sgr0 | xxd -g1
00000000: 1b 28 42 1b 5b 6d                                .(B.[m

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.

diff -u old new | sed "s/^-/$(tput setaf 1)&/; s/^+/$(tput setaf 2)&/; s/^@/$(tput setaf 6)&/; s/$/$(tput sgr0)/"

Unlike the other solutions to this question, this solution does not spell out the ANSI escape sequences explicitly. Instead, it invokes the tput setaf and tput 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:

for i in {0..255}; do tput setaf $i; printf %4d $i; done; tput sgr0; echo

Here is how the output looks:

enter image description here

Here is the evidence that the tput setaf and tput sgr0 commands generate the appropriate ANSI escape sequences:

$ tput setaf 1 | xxd -g1
00000000: 1b 5b 33 31 6d                                   .[31m
$ tput setaf 2 | xxd -g1
00000000: 1b 5b 33 32 6d                                   .[32m
$ tput setaf 6 | xxd -g1
00000000: 1b 5b 33 36 6d                                   .[36m
$ tput sgr0 | xxd -g1
00000000: 1b 28 42 1b 5b 6d                                .(B.[m
夜雨飘雪 2025-01-01 18:27:12

由于 wdiff 接受在插入和删除的开头和结尾指定字符串的参数,因此您可以使用 ANSI 颜色序列 作为这些字符串:

wdiff -n -w 

例如,这是比较两个 CSV 文件的输出:

diff CSV 文件输出

示例2.2 wdiff 的实际示例用法

\033[30;41m' -x

例如,这是比较两个 CSV 文件的输出:

diff CSV 文件输出

示例2.2 wdiff 的实际示例用法

\033[0m' -y

例如,这是比较两个 CSV 文件的输出:

diff CSV 文件输出

示例2.2 wdiff 的实际示例用法

\033[30;42m' -z

例如,这是比较两个 CSV 文件的输出:

diff CSV 文件输出

示例2.2 wdiff 的实际示例用法

\033[0m' file1 file2

例如,这是比较两个 CSV 文件的输出:

diff 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:

wdiff -n -w 

For example, this is the output of comparing two CSV files:

diff output of CSV files

Example from 2.2 Actual examples of wdiff usage.

\033[30;41m' -x

For example, this is the output of comparing two CSV files:

diff output of CSV files

Example from 2.2 Actual examples of wdiff usage.

\033[0m' -y

For example, this is the output of comparing two CSV files:

diff output of CSV files

Example from 2.2 Actual examples of wdiff usage.

\033[30;42m' -z

For example, this is the output of comparing two CSV files:

diff output of CSV files

Example from 2.2 Actual examples of wdiff usage.

\033[0m' file1 file2

For example, this is the output of comparing two CSV files:

diff output of CSV files

Example from 2.2 Actual examples of wdiff usage.

泪之魂 2025-01-01 18:27:12

到目前为止,还没有人提到 delta。它支持带有语法突出显示的语法彩色差异视图。

图片来源:达美航空

No one has mentioned delta so far. It supports syntax colored diff view with syntax highlighting.

Image source: Delta

白龙吟 2025-01-01 18:27:12

字符级颜色差异:
安装 ccdiff

ccdiff -r /usr/share/dict/words /tmp/new-dict

ccdiff 的输出

Character-level color diff:
Install ccdiff

ccdiff -r /usr/share/dict/words /tmp/new-dict

Output of ccdiff

烟雨扶苏 2025-01-01 18:27:12

我建议您尝试 diff-so-fancy 。我在工作期间使用它,目前看起来确实很棒。它包含许多选项,并且可以轻松按照您想要的方式配置差异。

您可以通过以下方式安装它:

sudo npm install -g diff-so-fancy

或在 Mac 上:

brew install diff-so-fancy

然后,您可以像这样突出显示您的差异:

diff -u file1 file2 | diff-so-fancy

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:

sudo npm install -g diff-so-fancy

or on Mac:

brew install diff-so-fancy

Afterwards, you can highlight your diffs like this:

diff -u file1 file2 | diff-so-fancy
花开半夏魅人心 2025-01-01 18:27:12
diff --color=always file_a file_b | less

为我工作

diff --color=always file_a file_b | less

works for me

我三岁 2025-01-01 18:27:12

使用 bat 命令:

diff file1 file2 | bat -l diff

With the bat command:

diff file1 file2 | bat -l diff
暗恋未遂 2025-01-01 18:27:12

在 Ubuntu 上最新版本的 Git 上,您可以通过以下方式启用差异突出显示:

sudo ln -s /usr/share/doc/git/contrib/diff-highlight/diff-highlight /usr/local/bin
sudo chmod a+x /usr/share/doc/git/contrib/diff-highlight/diff-highlight

然后将其添加到您的 .gitconfig 文件中:

[pager]
    log = diff-highlight | less
    show = diff-highlight | less
    diff = diff-highlight | less

该脚本可能位于其他发行版的其他位置。您可以使用 locate diff-highlight 来查找位置。

On recent versions of Git on Ubuntu, you can enable diff-highlighting with:

sudo ln -s /usr/share/doc/git/contrib/diff-highlight/diff-highlight /usr/local/bin
sudo chmod a+x /usr/share/doc/git/contrib/diff-highlight/diff-highlight

And then adding this to your .gitconfig file:

[pager]
    log = diff-highlight | less
    show = diff-highlight | less
    diff = diff-highlight | less

It's possible the script is located somewhere else in other distributions. You can use locate diff-highlight to find out where.

世态炎凉 2025-01-01 18:27:12

我最喜欢的选择是 vdiff ; 函数(我忘了从哪里得到它)。

它将在 Vim 中并排打开两个窗口,以便清楚地看到查看两个文件之间的差异。

vdiff () {
    if [ "${#}" -ne 2 ] ; then
        echo "vdiff requires two arguments"
        echo "  comparing dirs:  vdiff dir_a dir_b"
        echo "  comparing files: vdiff file_a file_b"
        return 1
    fi

    local left="${1}"
    local right="${2}"

    if [ -d "${left}" ] && [ -d "${right}" ]; then
        vim +"DirDiff ${left} ${right}"
    else
        vim -d "${left}" "${right}"
    fi
}

将此脚本放入您的 (.alias) 或 (.zshrc) 中,然后使用
vdiff <文件1> <文件2>。

示例

Enter此处的图像描述

结果为:

在此处输入图像描述

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.

vdiff () {
    if [ "${#}" -ne 2 ] ; then
        echo "vdiff requires two arguments"
        echo "  comparing dirs:  vdiff dir_a dir_b"
        echo "  comparing files: vdiff file_a file_b"
        return 1
    fi

    local left="${1}"
    local right="${2}"

    if [ -d "${left}" ] && [ -d "${right}" ]; then
        vim +"DirDiff ${left} ${right}"
    else
        vim -d "${left}" "${right}"
    fi
}

Put this script in your (.alias) or (.zshrc), and then call it using
vdiff <file1> <file2>.

Example

Enter image description here

The results are:

Enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文