In my current job I often have to work with files from Windows machines which most of the time isn't a big deal, but when piping a side-by-side diff to less, not only are the ^M being displayed, but it also messes up the indentation, like in the following:

<U+FEFF>using System;^M                                 <U+FEFF>using System;^M                                 
using System.Reflection;^M                                      using System.Reflection;^M                                      
using System.Runtime.Serializa^M                                        using System.Runtime.Serializa^M                                        
using System.Transactions;^M                                    using System.Transactions;^M                                    

(I don't particularly mind the UTF-8 BOM in the first line, as it affects only that one line.)

I know I can do a

diff -y <(tr -d '\015' < file-a) <(tr -d '\015' < file-b) | less

But that's a heck of a lot to type, and when file-a and file-b share a long path, you can't use bash's curly braces nicety. And

diff -y file-{a,b} | tr -d '\015' | less

does not do the trick, as the formatting is already messed up.

Interestingly though, the following displays fine both in terms of ^M and indentation:

diff -y file-{a,b} | head

So my question is, how do I get side-by-side diffs piped into less without the aforementioned issues? (Like adding some parameter to diff or less that I'm not aware of)

  • What does the display in diff -y file-{a,b} | tr -d '\015' | less look like? It lines up for me. By the way, if you want to trim ^M when they're passed to less, you can set the environment variable LESSOPEN='|-tr -d \\r' Commented Dec 10, 2014 at 0:42
  • @Gilles It looks the same as the diff output posted in my question - except the ^Ms are gone. If that wasn't so, I could use your LESSOPEN. Commented Dec 17, 2014 at 3:00

1 Answer 1


If you don't to modify files, just print differences, then one option is to simple ignore all white spaces with -w option, so that you can use your braces normally

diff -wy file-{a,b} | less

Of course this method only works if there are no other important changes in whitespace.

If your intention is to repair the files then I would recommend to use dos2unix utility.

  • Unfortunately, that does not fix the issues. It merely changes whether diff considers lines different that differ in whitespace only. Commented Dec 9, 2014 at 4:33
  • @EugeneBeresovksy I'm not sure what exactly are you trying to do, but perhaps dos2unix converter is what you are looking for.
    – jimmij
    Commented Dec 9, 2014 at 4:39

You must log in to answer this question.

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