sponsor Vim development Vim logo Vim Book Ad

diffchar.vim : Highlight the exact differences, based on characters and words

 script karma  Rating 35/11, Downloaded by 641    Comments, bugs, improvements  Vim wiki

created by
Rick Howe
script type
This script has been developed in order to make diff mode more useful. DiffText does not show the exact difference, but this script will highlight its difference, character by character - so called DiffChar.

Use this script just after diff command. DiffText area will be narrowed down to show the DiffChar. You can use this script in non-diff'ed usual mode as well.

For example, diff command shows: (<|DiffText area|>)

     (file A) The <|swift brown fox jumped over the lazy|> dog.
     (file B) The <|lazy fox jumped over the swift brown|> dog.

then this script will narrow down the DiffText area:

     (file A) The <|swift brown|> fox jumped over the <|lazy|> dog.
     (file B) The <|lazy|> fox jumped over the <|swift brown|> dog.

Sample commands
:[range]SDChar - Highlight difference units for [range]
:[range]RDChar - Reset the highlight of difference units for [range]

Sample keymaps
<F7> - toggle the highlight/reset of difference units for all lines
<F8> - toggle the highlight/reset of difference units for current line
[b   - jump cursor to the start position of the previous difference unit
]b   - jump cursor to the start position of the next difference unit
[e   - jump cursor to the end position of the previous difference unit
]e   - jump cursor to the end position of the next difference unit

Global variables (and tab local variables when using t:)
g:DiffUnit - type of difference unit
    "Char"   : any single character (default)
    "Word1"  : \w\+ word and any \W single character
    "Word2"  : non-space and space words
    "Word3"  : \< or \> character class boundaries
    "CSV(,)" : separated by a character such as ',', ';', and '\t'
g:DiffColors - matching colors for changed unit pairs
    0   : always DiffText (default)
    1   : 4 colors in fixed order
    2   : 8 colors in fixed order
    3   : 16 colors in fixed order
    100 : all available colors in dynamic random order
        (notes : always DiffAdd for added units)
g:DiffUpdate - interactively updating of highlightings while editing
    0 : disable (default)
    1 : enable
      (notes : available on vim 7.4)
g:DiffAlgorithm - difference algorithm
    "ONP"   : S.Wu, U.Manber, G.Myers and W.Miller,
              "An O(NP) Sequence Comparison Algorithm" (default)
    "OND"   : E.W.Myers, "An O(ND) Difference Algorithm and Its Variations"
    "Basic" : basic algorithm using edit graph and shortest edit distance

This script has been always positively supporting mulltibyte characters.

Update : 4.6
* Fixed to correctly show the colors of changed units in one-by-one defined order of g:DiffColors. Since an added unit was improperly counted as changed one, some colors were skipped and not shown. The first changed unit is now always highlighted with DiffText in any color mode.
install details
Load with :so command or copy to your vimrc file.

rate this script Life Changing Helpful Unfulfilling 
script versions (upload new version)

Click on the package to download.

package script version date Vim version user release notes
diffchar.vim.vim 4.6 2014-11-26 7.0 Rick Howe Fixed to correctly handle the color order and always highlight the first changed unit with DiffText in any color mode of g:DiffColors.
diffchar.vim 4.5 2014-11-08 7.0 Rick Howe Fixed to show the last "brown" on above example as added (not changed) units and to use your global variables defined in vimrc.
diffchar.vim 4.4 2014-07-21 7.0 Rick Howe Enhanced to follow diffopt's icase/iwhite options and to draw faster with a new function.
diffchar.vim 4.3 2014-07-03 7.0 Rick Howe Enhanced to differently show added/deleted/changed difference units with original diff highlightings.
diffchar.vim 4.2 2014-06-20 7.0 Rick Howe Enhanced to update the highlighted DiffChar units while editing.
diffchar.vim 4.1 2014-06-14 7.0 Rick Howe Implemented to echo a matching unit with its color when jumping cursor. And fixed a defect.
diffchar.vim 4.0 2014-06-11 7.0 Rick Howe Enhanced to easily find each difference unit pair on both windows.
diffchar.vim 3.6 2014-06-06 7.0 Rick Howe Added two difference unit types, "Word3" and "CSV(c)"
diffchar.vim 3.5 2014-06-01 7.0 Rick Howe Fixed defects: DiffChar highlighting units do not override/hide hlsearch.
diffchar.vim 3.4 2014-05-30 7.0 Rick Howe Enhanced to support individual DiffChar handling on each tab page.
diffchar.vim 3.3 2014-05-28 7.0 Rick Howe Enhanced to jump cursor to the DiffChar highlighting units.
diffchar.vim 3.2 2014-05-26 7.0 Rick Howe Enhanced to follow diff mode without any limitations.
diffchar.vim 3.1 2014-05-19 7.0 Rick Howe Enhanced for individual line by line highlightings and window layout handling.
diffchar.vim 3.0 2014-05-16 7.0 Rick Howe Implemented word by word differences.
diffchar.vim 2.1 2014-05-06 7.0 Rick Howe Coding changes in the O(NP) function for readability.
diffchar.vim 2.0 2014-05-05 7.0 Rick Howe Implemented the O(NP) and O(ND) Difference algorithms to improve the performance.
diffchar.vim 1.0 2014-05-02 7.0 Rick Howe Initial upload
ip used for rating:

If you have questions or remarks about this site, visit the vimonline development pages. Please use this site responsibly.
Questions about Vim should go to the maillist. Help Bram help Uganda.
SourceForge.net Logo