sponsor Vim development Vim logo Vim Book Ad

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

 script karma  Rating 43/13, Downloaded by 737    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, vim is able to automatically show the exact differences on diff mode.

This plugin has been always positively supporting mulltibyte characters.

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

Configurable sample 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 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

DiffCharExpr(iet, exd) function for the diffexpr option
    iet: 1 = internal algorithm, 0 = external diff command,
          else = threshold value of differences to apply either
    exd: 1 = initially show exact differences, 0 = vim original ones

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
 
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 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.144.240.110

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