showing differences within a line in diff output

2019-03-14 16:13发布

This StackOverflow answer has an image of KDiff3 highlighting intra-line differences. Does someone know of a tool which can show the same (ex, via color) on the command line?

Another way to think of this is wanting to diff each difference in a patch file.

5条回答
smile是对你的礼貌
2楼-- · 2019-03-14 16:34

I tried all the tools I found: wdiff, dwdiff, kdiff3, vimdiff to show the difference between two long and slightly different lines. My favourite is diff-highlight (part of git contrib)

  • it supports diff format - great advantage over tools requiring two files like (dwdiff), e.g. if you need to visualize the output of unit tests
  • it highlights with black+white or with color if you connect it to colordiff
  • highlights characterwise - helpful for comparing long lines without spaces (better than wdiff)

Installation

On Ubuntu, you probably already have it as part of git contrib (installed within the git deb package). Copy or link it into your ~/bin folder from /usr/share/doc/git/contrib/diff-highlight/diff-highlight

Usage example

cat tmp.diff | diff-highlight | colordiff

Result:

enter image description here

查看更多
唯我独甜
3楼-- · 2019-03-14 16:49

I don't know if this is sufficiently command line for your purpose, but vimdiff can do this (even does colour). See for example the image in this related question.

查看更多
【Aperson】
4楼-- · 2019-03-14 16:53

I had a similar problem and wanted to avoid using vimdiff. I found dwdiff (which is available in Debian) to have several advantages over wdiff.

The most useful feature of dwdiff is that you can customise the delimiters with -d [CHARS], so it's useful for comparing all kinds of output. It also has color built in with the -c flag.

查看更多
ゆ 、 Hurt°
5楼-- · 2019-03-14 16:54

Another intuitive way to see all word-sized differences (though not side-by-side) is to use wdiff together with colordiff (you might need to install both). An example of this would be:

wdiff -n {file-A} {file-A} | colordiff

You can optionally pipe this into less -R to scroll through the output (-R is used to show the colors in less).

查看更多
Luminary・发光体
6楼-- · 2019-03-14 16:56

You might be able to use colordiff for this.

In their man page:

Any options passed to colordiff are passed through to diff except for the colordiff-specific option 'difftype', e.g.

colordiff --difftype=debdiff file1 file2

Valid values for 'difftype' are: diff, diffc, diffu, diffy, wdiff, debdiff; these correspond to plain diffs, context diffs, unified diffs, side-by-side diffs, wdiff output and debdiff output respectively. Use these overrides when colordiff is not able to determine the diff-type automatically.

I haven't tested it, but the side-by-side output (as produced by diff -y file1 file2) might give you the equivalent of in-line differences.

查看更多
登录 后发表回答