1

When I use diff -y to compare two text files with long lines of text, the result cuts off each column and I cannot read an entire line. I would like instead the text to wrap (or fold as it is also called) so that I can read the whole line.

For example, say I have two simple text files one line each:

cat tmp.txt tmp2.txt
The quick brown fox jumped over the lazy dog. This line is very long and will not show when I use a diff. 
The quick brown fox did not jump over the lazy dog. This line is very long and will not show when I use a diff. 

diffing them I get,

diff -y tmp.txt tmp2.txt
The quick brown fox jumped over the lazy dog. This line is ve | The quick brown fox did not jump over the lazy dog. This line

which cuts off the lines in the middle.

I know about the -W option to make it wider, but the lines are longer than my screen width so this does not help. Using a simple diff does make the text wrap, but there is no text wrapping with diff -y.

So how can I use diff -y with text wrapping?

I'm using the terminal on a Mac, if that helps.

This seems like a simple thing to want, but after searching everywhere I cannot find a solution!

perror
  • 3,171
  • 7
  • 33
  • 45
xenkan
  • 11
  • 1
  • 2

3 Answers3

5

If it is ok to lose line number information , and you are just interested in visual diffs, you could fold the files first before diffing them.

diff -y <(fold -s -w72 file1) <(fold -s -w72 file2) -W 200

In this command we have used process substitution to first fold each file to line of 72 characters, but with -s option we ensure that the lines are broken only on the space character.

Once the lines are wrapped by fold, diff command would work on the wrapped output.

EDIT: specifying -W to diff with a larger value than that given in the fold will ensure that the diff and fold have sufficient space to work with each other. In this case -W 200 is given to diff

amisax
  • 2,957
  • 17
  • 23
  • 1
    Please also consider expanding width of `diff` command with `-W` option. It seemed handy when used along with your answer ,example: `diff -y <(fold -s -w72 file1) <(fold -s -w72 file2) -W 200`. Not all lines were folded properly with just `diff -y <(fold -s -w72 file1) <(fold -s -w72 file2)`. Also not all line were wrapped properly with just `diff -y <(fold -s -w72 file1) <(fold -s -w72 file2) -W 200`. – ss_iwe Aug 26 '19 at 06:04
  • Thank you so much! This works perfectly! – xenkan Sep 12 '19 at 21:38
  • Glad it worked for you. Do mark this the accepted answer in that case. – amisax Sep 13 '19 at 10:35
0

I like to use icdiff for this task.

The screenshot after running icdiff tmp.txt tmp1.txt:

icdiff outputs

zhanxw
  • 216
  • 3
  • 3
0

Kdiff3 has a setting Diffview > Word Wrap Diff Windows which breaks lines at the closest word boundary in the frame. It is the only tool I have found that does this.

kdiff3 showing diff of two texts with word wrap

Pum Walters
  • 111
  • 3