Colored word-diff just like git’s

colordiffcolorsdiff()git

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 {+, +}.

Best Answer

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