sponsor Vim development Vim logo Vim Book Ad

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

 script karma  Rating 50/16, Downloaded by 1402    Comments, bugs, improvements  Vim wiki

created by
Rick Howe
script type
This plugin has been developed in order to make diff mode more useful. Vim highlights all the text in between the changed first and last characters on changed lines. But this plugin will find the exact differences between them, character by character - so called DiffChar.

For example, in diff mode: ([DiffText], <DiffAdd>)

     (file A) The [quick brown fox jumps over the lazy] dog.
     (file B) The [lazy fox jumps over the quick brown] dog.

this plugin will exactly highlight the changed and added units:

     (file A) The [quick] <brown >fox jumps over the [lazy] dog.
     (file B) The [lazy] fox jumps over the [quick] <brown >dog.

This plugin can be triggered after diff mode starts by pressing <F7>/<F8> or using SDChar command. At update 5.5, a new global variable, g:DiffModeSync, is introduced. Its default is "enable" and synchronously show/reset the highlights of the exact differences as soon as the diff mode starts/ends. It also works on your custom diff tool (e.g. git-diff) when specified to the diffexpr option.

In diff mode, this plugin compares the corresponding changed lines between two windows. In non-diff mode, it compares the same lines between them.

This plugin has been using "An O(NP) Sequence Comparison Algorithm" developed by S.Wu, et al., which always finds an optimum sequence quickly. But for longer lines and less-similar files, it takes time to complete the diff tracing. At update 5.2, this plugin splits the tracing with the diff command. Firstly applies the internal O(NP) algorithm. If not completed within the time specified by a g:DiffSplitTime global (and tabpage) variable, continuously switches to the diff command at that point, and then joins both results. This approach provides a stable performance and reasonable accuracy, because the diff command effectively optimizes between them. The default of its variable is 500 ms, which would be useful for smaller files. If prefer to always apply the internal algorithm for accuracy (or the diff command for performance) only, set some large value (or 0) to the variable.

Since update 4.7, this plugin has set the DiffCharExpr() to the diffexpr option, if it is empty. this function would be rather useful for smaller files than the diff command. If the total number of lines on two windows <= 200, by default, this plugin's internal algorithm is used to make the diff faster. If prefer to leave the diffexpr option as empty, set 0 to g:DiffExpr.

This plugin has been always positively supporting mulltibyte characters.

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

Configurable Keymaps
<Plug>ToggleDiffCharAllLines (default: <F7>)
    toggle the highlight/reset of difference units for all lines
<Plug>ToggleDiffCharCurrentLine (default: <F8>)
    toggle the highlight/reset of difference units for current line
<Plug>JumpDiffCharPrevStart (default: [b)
    jump cursor to the start position of the previous difference unit
<Plug>JumpDiffCharNextStart (default: ]b)
    jump cursor to the start position of the next difference unit
<Plug>JumpDiffCharPrevEnd (default: [e)
    jump cursor to the end position of the previous difference unit
<Plug>JumpDiffCharNextEnd (default: ]e)
    jump cursor to the end position of the next difference unit

Global Variables (and Tabpage 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 characters 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
    1 : enable (default)
    0 : disable
      (notes : available on vim 7.4)
g:DiffSplitTime - a time length (ms) to apply the internal algorithm first
    0 ~ : (500 as default)
g:DiffModeSync - synchronously show/reset with diff mode
    1 : enable (default)
    0 : disable

DiffCharExpr(mxi) function for the diffexpr option
    mxi: the maximum number of total lines of both windows to apply internal algorithm, apply diff command when more lines

Update : 5.5
* Introduced g:DiffModeSync to synchronously show/reset the highlights as the diff mode starts/ends, which also works on your custom diff tool.
* Changed to enable g:DiffUpdate as a default and then interactively update the highlights while editing.
* Enhanced to draw and delete the highlights faster by specifying as many position parameters as possible in one matchaddpos() and matchadd() call.
* Changed to select current window and next diff mode window (if present) whose buffer is different at initialize.
* Fixed:
  - caused an error on getwinvar() in vim 7.3.
  - in non-gVim, did not show a matching pair cursor when jumping the cursor by using [e or ]b, depending on a color scheme.
  - sometimes failed to toggle the highlights when using <F7> or <F8> in diff mode windows.
  - did not interactively update the highlight of all the lines when multiple lines were changed at once on g:DiffUpdate = 1.
install details

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 5.5 2015-11-23 7.0 Rick Howe Introduced g:DiffModeSync to synchronously show/reset the highlights as the diff mode starts/ends. Changed to enable g:DiffUpdate as a default. Fixed several defects.
diffchar.vim 5.4 2015-10-06 7.0 Rick Howe Enhanced to show a position of a deleted unit with underline, and more.
diffchar.vim 5.3 2015-09-06 7.0 Rick Howe Performance improved for long lines and some defects fixed when the diff command is used for the diff tracing.
diffchar.vim 5.2 2015-08-15 7.0 Rick Howe Enhanced to provide a stable performance by splitting the diff tracing with this plugin's algorithm and the diff command.
diffchar.vim 5.1 2015-06-01 7.0 Rick Howe A workaround for patch 7.4.682, diff highlights can not be overwritten and will be left in all the lines.
diffchar.vim 5.0 2015-05-07 7.0 Rick Howe Improved to be 1.5 ~ 2.0 times faster. Introduced g:DiffMaxRatio to keep performance for long less-similar lines.
diffchar.vim 4.9 2015-02-18 7.0 Rick Howe Fixed DiffCharExpr() to keep the appropriate performance for large files.
diffchar.vim 4.81 2015-01-13 7.0 Rick Howe Enhanced to make DiffCharExpr() a bit faster by using uniq() or so.
diffchar.vim 4.8 2015-01-12 7.0 Rick Howe Enhanced to set the threshold for diffexpr option to switch internal/external to be faster on less differences and stable on many ones. Changed the way to select windows among more than 2 windows.
diffchar.vim 4.7 2014-12-26 7.0 Rick Howe Enhanced to use faster this plugin's diff algorithm and initially show exact differences on diff mode. Enhanced to make the key mappings configurable. Fixed to correctly adjust the iwhite unit position.
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