I use vimdiff to inspect differences between files, and some times it misses out a bit. for example, if I have the following two files:



int i = printf("hello\n") + 3;

showing them in vimdiff gives:

enter image description here

What I will want to see is that in b.c the h is not capitalized.

Is there a way to tell vimdiff to compare (in that line) just part of it (I mean, ignore the int i = and the + 3)?

  • 5
    The diffchar plugin is probably what you need but I have zero experience with it. Maybe someone with experience using it can give a good, detailed answer (or maybe an alternative).
    – tokoyami
    Feb 6 '15 at 11:30

I don't think this can be done by Vim itself. From :h view-diffs (emphasis mine):

hl-DiffText   DiffText        Changed text inside a Changed line.  Vim
                                finds the first character that is different,
                                and the last character that is different
                                (searching from the end of the line).  The
                                text in between is highlighted.  This means
                                that parts in the middle that are still the
                                same are highlighted anyway.  Only "iwhite" of
                                'diffopt' is used here.

Tokoyami's suggestion takes us one step closer. 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
  • +1 but "one step closer" seems overly conservative to me. Diffchar totally solves this issue IMO.
    – Sparhawk
    Jul 8 '16 at 12:24
  • @Sparhawk well, it doesn't ignore the common elements in a line (OP wants just the changes highlighted)
    – muru
    Jul 8 '16 at 13:00
  • I'm not sure that I understand. Aren't the common elements ignored in this example (e.g. printf and ello)?
    – Sparhawk
    Jul 8 '16 at 22:10
  • 1
    @Sparhawk in this example, yes. But with more complex changes, it quickly becomes messy.
    – muru
    Jul 9 '16 at 7:44
  • Ah okay, so it's a bit buggy then… thanks for the information.
    – Sparhawk
    Jul 9 '16 at 10:02

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.