sponsor Vim development Vim logo Vim Book Ad

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

 script karma  Rating 46/15, Downloaded by 1024    Comments, bugs, improvements  Vim wiki

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

Use this plugin just after diff command. DiffText area will be narrowed down to show the DiffChar. You can use this plugin in non-diff 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 plugin 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.

Since update 4.7, this plugin has set the DiffCharExpr() to the diffexpr option, if it is empty. This function would be useful for smaller files. If the total number of lines on both diff windows <= 200, by default, it applies the internal difference algorithm to make the diff faster. And, by default, it initially shows the exact differences for all lines whenever diff mode begins. You can change these arguments of this function like "set diffexpr=DiffCharExpr(100, 0)", but if prefer not to use this enhancement, set g:DiffExpr = 0.

Until update 5.0, this plugin has always traced the exact differences. But for long and less-similar files and lines, it may take time to complete. At 5.0, the g:DiffMaxRatio global (and tabpage) variable, which is an assumption of how much % the differences exist at a maximum, is introduced. Once the difference ratio actually exceeds g:DiffMaxRatio while tracing, this plugin recursively splits the tracing at that unit and then joins each results. Its default is 100%, meaning it still finds the exact differences as before. Try to decrease this ratio if performance is more important than diff accuracy.

This plugin has been always positively supporting mulltibyte characters.

Commands
:[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
    0 : disable (default)
    1 : enable
      (notes : available on vim 7.4)
g:DiffMaxRatio - a maximum difference ratio to trace
    0 ~ 100 : (100% as default)

DiffCharExpr(mxi, exd) function for the diffexpr option
    mxi: the maximum number of total lines of both windows to apply internal algorithm, apply external diff command when more lines
    exd: 1 = initially show exact differences, 0 = vim original ones

Update : 5.0
* Significantly improved the way to trace and show the differences and make them 1.5 ~ 2.0 times faster.
* Introduced g:DiffMaxRatio (and t:DiffMaxRatio), a maximum difference ratio to trace (100% as default). Once exceeds, the diff tracing is recursively split and helps to keep performance instead of diff accuracy.
* Discontinued other difference algorithms (OND and Basic) than the ONP, then g:DiffAlgorithm no longer supported.
* Improved to allow to specify one or more characters for "CSV(c)" in g:DiffUnit (and t:DiffUnit). For example, "CSV(,:\t)" will split the units by a comma, colon, and tab. Use '\' for a backslash.

Update : 4.9
* Fixed DiffCharExpr() to check the number of total lines, not different lines only, of both windows and apply either internal algorithm or external diff command, in order to keep the appropriate performance for large files.

Update : 4.81
* Enhanced to make DiffCharExpr() a bit faster by using uniq() or so.

Update : 4.8
* Enhanced to set the threshold value on DiffCharExpr() to check how many differences and then apply either of internal algorithm or external diff command. The default for diffexpr option using DiffCharExpr() is changed to use this threshold, 200 - apply internal if less than 200 differences, apply external if more.
* Changed the way to select windows when more than 2 windows in the page.
  - automatically select the diff mode's next (wincmd w) window, if any, in addition to the current window
  - can select any of splitted windows as vim can do for diff

Update : 4.7
* Enhanced to set DiffCharExpr() to the diffexpr option, if it is empty. When diff mode begins, vim calls this function which finds differences by this plugin's internal diff algorithm (default) and then initially shows the exact differences (default). You can also explicitly set this function to the option with different arguments.
* Enhanced to make the key mappings configurable. For example, the default <F7> can be modified by:
       nmap <silent> "your favorite key" <Plug>ToggleDiffCharAllLines
* Fixed to correctly adjust the position of difference units when diffopt's iwhite option is enabled.
 
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.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: 54.166.55.74

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