sponsor Vim development Vim logo Vim Book Ad

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

 script karma  Rating 22/7, Downloaded by 448    Comments, bugs, improvements  Vim wiki

created by
Rick Howe
 
script type
utility
 
description
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.4
* Enhanced to follow diffopt's icase and iwhite options for both diff and non-diff modes (ignorecase option is not used). Previously, it has been always case and space/tab sensitive.
* Implemented to highlight the difference units using a new matchaddpos() function, introduced in 7.4.330, when available to draw faster.

Update : 4.3
* Enhanced to differently show added/deleted/changed difference units with original diff highlightings.
- added units will be always highlighted with DiffAdd.
- changed units will be highlighted based on the g:DiffColors (and t:DiffColors) variable, but DiffText is always used for the first changed unit.
- when jumping cursor by "[b"/"]b" or "[e"/"]e" on the added unit, it highlights around the corresponding deleted units with a cursor-type color in another window, and echoes a diff-delete filler with DiffDelete, along with common characters on both sides (e.g. a-----b).

Update : 4.2
* Enhanced to update the highlighted DiffChar units while editing. A g:DiffUpdate (and t:DiffUpdate) variable enables and disables (default) this update behavior. If a text line was added/deleted, reset all the highlightings. This feature is available on vim 7.4.

Update : 4.1
* Implemented to echo a matching difference unit with its color when jumping cursor by "[b"/"]b" or "[e"/"]e".
* Fixed defects: not using the new uniq() function introduced in vim 7.4.

Update : 4.0
* Enhanced to easily find a corresponding pair of each difference unit.
  - each unit pair will be shown in individual same color on both windows. A g:DiffColors (and t:DiffColors) variable is a type of matching colors, 0 (default) for always 1 color as before, 1/2/3 for 4/8/16 colors in fixed order, and 100 for all available colors in dynamic random order.
  - when jumping cursor by "[b"/"]b" or "[e"/"]e" in either window, the start or end position of a matching unit will be highlighted with a cursor-type color in another window.

Update : 3.6
* Added two g:DiffUnit (and t:DiffUnit) types. "Word3" will split at the \< or \> boundaries, which can separate based on the character class like CJK, Hiragana, Katakana, Hangul, full width symbols and so on. And "CSV(c)" will split the units by a specified character "c". For example, "CSV(,)" and "CSV(\t)" can be used for comma and tab separated text.

Update : 3.5
* Fixed defects: DiffChar highlighting units do not override/hide hlsearch.

Update : 3.4
* Enhanced to support individual DiffChar handling on each tab page. Difference unit and algorithm can also be set page by page using tab page local variables, t:DiffUnit and t:DiffAlgorithm.

Update : 3.3
* Enhanced to jump cursor to the DiffChar highlighting units. Sample keymaps "]b" and "[b" will move cursor forwards to the next and backwards to the previous start positions. And "]e" and "[e" will move to the end positions.

Update : 3.2
* Enhanced to follow diff mode without any limitations. Compare between the corresponding DiffChange lines on both windows and properly handle DiffAdd and DiffDelete lines.

Update : 3.1
* Enhanced to show/reset/toggle DiffChar highlightings on individual line by line.
* Implemented the window layout handling.
  - the DiffChar'ed windows will remain the highlightings even if the window position is rotated/replaced/moved and another new window opens.
  - if either DiffChar'ed window is closed, reset all the DiffChar highlightings on another window.
* Removed limitations:
  - when more than 2 windows exist, current and next (wincmd w) windows will be selected.
  - if the specified numbers of lines are different in both windows, ignore the redundant lines and continue to compare the text on the same lines.
  - RDChar sample command has a range attribute (e.g. %RDChar).
* Fixed defects:
  - reset just DiffChar highlightings only and remain others.

Update : 3.0
* Implemented word by word differences. A g:DiffUnit variable is a type of a difference unit. Its default is "Char", which will trace character by character as before. "Word1" will split into \w\+ words and any \W single characters. And "Word2" will separate the units at the \s\+ space boundaries.
* Improved the performance around 10%.

Update : 2.1
* Coding changes in the O(NP) function for readability.

Update : 2.0
* Implemented the O(NP) and O(ND) Difference algorithms to improve the performance. This update uses the O(NP) by default, and can be changed to the O(ND) if necessary, or to the basic algorithm implemented in the initial version.
 
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.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.80.121.186

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