0

I'm on OSX and running a diff command like this, to create side-by-side output:

$] diff -Bbwy --width=200 --suppress-common-lines file1.txt file2.txt > diff-output.txt

It outputs my diffs just fine, but it uses tabs for indentation of the output (alignment of the side-by-side middle line, etc). Additionally, the files in question also use tabs for indentation inside them, so the diff output has all these tab characters in them.

I want the output of diff to have spaces, specifically 4-space's instead of tabs.

I know diff supports the -t option to do just that, but the problem is it uses a fixed size of 8-spaces for all tabs (not only the leading line indentation tabs from the original files, but also its alignment tabs to keep the middle line aligned, etc).

I've tried a variety of post-processing approaches to take the tabs (or spaces) from diff output and shrink them to the desired size of 4-spaces, but that always just causes mis-alignments of the diff side-by-side output.

Then I thought maybe I could pre-process the two files being diffed, using tab2space or something like that, to get each file's line indentations from tabs to 4-spaces before diffing. But I can't work out how, without temporary files, to then get both of those pre-processed inputs piped into diff.

Can that be done?

Or is there any other approach I've not tried? Any secret way to config/force diff -t to just use 4-space tabs instead of 8?

  • BTW, I believe that -bw is redundant. Do you find that it behaves differently from -b? – G-Man Nov 5 '17 at 19:51
  • I don't know that I'm seeing any difference between -b, -w, and -bw, but they seemed like they could be different as they're listed separately. Shrugs. – Kyle Simpson Nov 6 '17 at 20:45
2

diff  (options)  <(expand -t4 file1.txt)  <(expand -t4 file2.txt)
will expand tabs in the input files to spaces, assuming tab stops set every four columns.  Combining this with diff’s -t option should get you what you want.

  • Awesome, that worked perfectly. I was trying | and had completely forgotten about <. :) – Kyle Simpson Nov 6 '17 at 20:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.