Search code examples
linuxunixdiffsdiff

Unix diff side-by-side with context


In Unix, execute

diff -y file1 file2

can see two files side-by side. And execute

diff -c file1 file2

can see two files' difference with 3 line context. Execute

diff -y --supress-common-lines

can suppress all the common lines. But what if I want to display side-by-side but with 3 lines context? --supress-common-lines will provide no context at all

diff -y -c file1 file2

will give me conflicting output style options. Is there a way to achieve that?


Solution

  • It seems that diff(1) doesn't allow it, but vimdiff seems to work:

    vimdiff -c 'set diffopt=context:3' file1 file2
    

    Downside is that it's interactive-only, you can't dump the diff to a file, but then again, side-by-side diffs are not very useful in files. However, if you do want to save it to a file, this awesome answer will have you do:

    vimdiff -c 'set diffopt=context:3' -c TOhtml -c 'w! output.html' -c 'qa!' file1 file2
    

    Not ideal, but it's something.