10

Is there a stable tool (option/plugin of an existing tool: vimdiff, diff, etc) in Linux to do diff between two text files characterwise?

I would like to see the longest common subsequence between my two files highlighted. There is a classic polynomial-time algorithm for that, but the standard tools seem to only work line by line.

Gilles 'SO- stop being evil'
  • 807,993
  • 194
  • 1,674
  • 2,175
Valentas
  • 349
  • 2
  • 9

2 Answers2

2

Good old cmp does a characterwise diff. It's been a part of UNIX for about 40 years. https://www.freebsd.org/cgi/man.cgi?query=cmp&apropos=0&sektion=0&manpath=Unix+Seventh+Edition&arch=default&format=html

Unfortunately, it stops at the first differing character, so I don't know if this answer is useful for you.

Dominik R
  • 289
  • 1
  • 8
  • 1
    Thanks. I just ran a `cmp -bl ...` after finding this answer and it reported multiple differences and their positions. My version is `cmp (GNU diffutils) 3.6`. – Dzamo Norton Dec 03 '18 at 17:49
2

Install diffchar (Github link for those using a plugin autoloader). Since v4.7, with the plugin enabled, Vim automatically shows more exact differences:

enter image description here

  • You can press F7 (the default keymap) to toggle between the usual and the plugin. Reassign with:

    nmap <silent> <key> <Plug>ToggleDiffCharAllLines
    

    replacing <key> with an appropriate key code.

  • You can use more colors:

    let g:DiffColors=100
    

Unfortunately, this doesn't work across lines, best I can tell. However, common regions are left un-highlighted (and folded, if they grow too long), so you might be able to take advantage of that.


Copied from my Vi and Vim Stack Exchange post.

muru
  • 69,900
  • 13
  • 192
  • 292