8

I've been for long using git's (colored) --word-diff which I find awesome. I found myself today in a situation where I had two strings I needed to compare word by word and craving for something just like git's diff.

I am aware of wdiff + colordiff, but its results certainly aren't stellar:

me@me:~$ wdiff <(echo -e "abc\ndef") <(echo -e "dbcx\ndef") | colordiff
[-abc-]{+dbcx+}
def

Is there any better option around? I don't particularly like those [-, -] and {+, +}.

6

You can use git's own diff-highlight. It can highlight word differences in unified diff output, and it can also cope with ANSI colors on input. So you can do something like this:

colordiff -u <(echo -e "foo abc\ndef") <(echo -e "foo dbcx\ndef") | diff-highlight
  • That only works for the trivial case where there is a single word changed on the line. When it gets multiple words changing, it incorrectly stretches the reverse-video across words which did not change: colordiff -u <(echo -e "foo abc x y\ndef") <(echo -e "foo dbcx x z\ndef") | diff-highlight – Thomas Dickey Sep 2 '16 at 0:25
  • @ThomasDickey It seems to need two words to re-synch. Anyway, it is what git does. shrug – Satō Katsura Sep 2 '16 at 4:12

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.