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!

2
  • 1
    "I'm using the terminal on a Mac" and yet this is tagged "Linux"
    – muru
    Commented Aug 26, 2019 at 4:39
  • if it is ok to lose original line numbers, if you just want to see the visual diff ?
    – amisax
    Commented Aug 26, 2019 at 5:13

3 Answers 3

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

3
  • 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
    Commented Aug 26, 2019 at 6:04
  • Thank you so much! This works perfectly!
    – xenkan
    Commented Sep 12, 2019 at 21:38
  • Glad it worked for you. Do mark this the accepted answer in that case.
    – amisax
    Commented Sep 13, 2019 at 10:35
0

I like to use icdiff for this task.

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

icdiff outputs

2
  • vimdiff is my prefrence Commented Dec 15, 2019 at 5:43
  • Icdiff works to some degree, but if lines on the right are longer than those on the left, just a newline is inserted so that the end of the right-hand line appears on the left. Very confusing. Commented Jul 21, 2021 at 7:16
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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .