Mini.diff
Work with diff hunks. Part of 'mini.nvim' library.
Install / Use
/learn @nvim-mini/Mini.diffREADME
Work with diff hunks
See more details in Features and Documentation.
[!NOTE] This was previously hosted at a personal
echasnovskiGitHub account. It was transferred to a dedicated organization to improve long term project stability. See more details here.
⦿ This is a part of mini.nvim library. Please use this link if you want to mention this module.
⦿ All contributions (issues, pull requests, discussions, etc.) are done inside of 'mini.nvim'.
⦿ See whole library documentation to learn about general design principles, disable/configuration recipes, and more.
⦿ See MiniMax for a full config example that uses this module.
If you want to help this project grow but don't know where to start, check out contributing guides of 'mini.nvim' or leave a Github star for 'mini.nvim' project and/or any its standalone Git repositories.
Demo
<!-- Demo source: https://github.com/nvim-mini/assets/blob/main/demo/demo-diff.mp4 -->https://github.com/nvim-mini/mini.nvim/assets/24854248/77849127-ee9f-430b-9eff-5a8a724c21ea
Features
-
Visualize difference between buffer text and its configurable reference interactively (updates as you type). This is done per line showing whether it is inside added, changed, or deleted part of difference (called hunk). Visualization can be with customizable colored signs or line numbers.
-
Special toggleable overlay view with more hunk details inside text area.
-
Completely configurable per buffer source(s) of reference text used to keep it up to date and define interactions with it. Can be array of sources which are attempted to attach in order. By default uses Git source (buffer's file text from Git index as reference).
-
Configurable mappings to manage diff hunks:
- Apply and reset hunks inside region (selected visually or with a dot-repeatable operator).
- "Hunk range under cursor" textobject to be used as operator target.
- Navigate to first/previous/next/last hunk.
What it doesn't do:
- Provide functionality to work directly with Git outside of visualizing and staging (applying) hunks with (default) Git source. In particular, unstaging hunks is not supported.
For more information see these parts of help:
:h MiniDiff-overview:h MiniDiff-source-specification:h MiniDiff-hunk-specification:h MiniDiff-diff-summary
Overview
Diffs and hunks
The "diff" (short for "difference") is a result of computing how two text strings differ from one another. This is done on per line basis, i.e. the goal is to compute sequences of lines common to both files, interspersed with groups of differing lines (called "hunks").
Although computing diff is a general concept (used on its own, in Git, etc.), this module computes difference between current text in a buffer and some reference text which is kept up to date specifically for that buffer. For example, default reference text is computed as file content in Git index. This can be customized in config.source.
Life cycle
- When entering proper (not already enabled, valid, showing text) buffer, it is attempted to be enabled for diff processing.
- During enabling, attempt attaching the source. This should set up how reference text is kept up to date.
- On every text change, diff computation is scheduled in debounced fashion after customizable delay (200 ms by default).
- After the diff is computed, do the following:
- Update visualization based on configurable style: either by placing colored text in sign column or coloring line numbers. Colors for both styles are defined per hunk type in corresponding
MiniDiffSign*highlight group and sign text for "sign" style can be configured inconfig.view.signs. - Update overlay view (if it is enabled).
- Update
vim.b.minidiff_summaryandvim.b.minidiff_summary_stringbuffer-local variables. These can be used, for example, in statusline. - Trigger
MiniDiffUpdatedUserevent. See:h MiniDiff-diff-summaryfor example of how to use it.
- Update visualization based on configurable style: either by placing colored text in sign column or coloring line numbers. Colors for both styles are defined per hunk type in corresponding
Notes:
- Use
:editto reset (disable and re-enable) current buffer.
Overlay
Along with basic visualization, there is a special view called "overlay". Although it is meant for temporary overview of diff details and can be manually toggled via MiniDiff.toggle_overlay(), text can be changed with overlay reacting accordingly.
It shows more diff details inside text area:
-
Added buffer lines are highlighted with
MiniDiffOverAddhighlight group. -
Deleted reference lines are shown as virtual text and highlighted with
MiniDiffOverDeletehighlight group. -
Changed reference lines are shown as virtual text and highlighted with
MiniDiffOverChangehighlight group."Change" hunks with equal number of buffer and reference lines have special treatment and show "word diff". Reference line is shown next to its buffer counterpart and only changed parts of both lines are highlighted with
MiniDiffOverChange. The rest of reference line hasMiniDiffOverContexthighlighting.This usually is the case when
config.options.linematchis enabled.
Mappings
This module provides mappings for common actions with diffs, like:
- Apply and reset hunks.
- "Hunk range under cursor" textobject.
- Go to first/previous/next/last hunk range.
Examples:
vipfollowed bygh/gHapplies/resets hunks inside current paragraph. Same can be achieved in operator formghip/gHip, which has the advantage of being dot-repeatable.gh_/gH_applies/resets current line (even if it is not a full hunk).ghgh/gHghapplies/resets hunk range under cursor.dghdeletes hunk range under cursor.[H/[h/]h/]Hnavigate cursor to the first / previous / next / last hunk range of the current buffer.
Mappings for some functionality are assumed to be done manually. See :h MiniDiff.operator().
Buffer-local variables
Each enabled buffer has the following buffer-local variables which can be used in custom statusline to show an overview of hunks in current buffer:
-
vim.b.minidiff_summaryis a table with the following fields:source_name- name of the source.n_ranges- number of hunk ranges (sequences of contiguous hunks).add- number of added lines.change- number of changed lines.delete- number of deleted lines.
-
vim.b.minidiff_summary_stringis a string representation of summary with a fixed format. It is expected to be used as is. To achieve different formatting, usevim.b.minidiff_summaryto construct one. The best way to do this is by overridingvim.b.minidiff_summary_stringin the callback forMiniDiffUpdatedevent:local format_summary = function(data) local summary = vim.b[data.buf].minidiff_summary local t = {} if summary.add > 0 then table.insert(t, '+' .. summary.add) end if summary.change > 0 then table.insert(t, '~' .. summary.change) end if summary.delete > 0 then table.insert(t, '-' .. summary.delete) end vim.b[data.buf].minidiff_summary_string = table.concat(t, ' ') end local au_opts = { pattern = 'MiniDiffUpdated', callback = format_summary } vim.api.nvim_create_autocmd('User', au_opts)
Installation
This plugin can be installed as part of 'mini.nvim' library (recommended) or as a standalone Git repository.
There are two branches to install from:
main(default, recommended) will have latest development version of plugin. All changes since last stable release should be perceived as being in beta testing phase (meaning they already passed alpha-testing and are moderately settled).stablewill be updated only upon releases with code tested during public beta-testing phase inmainbranch.
Here are code snippets for some common installation methods (use only one):
<details> <summary>With <a href="https://nvim-mini.org/mini.nvim/readmes/mini-deps">mini.deps</a></summary>-
'mini.nvim' library:
| Branch | Code snippet | |--------|-----------------------------------------------| | Main | Follow recommended 'mini.deps' installation | | Stable | Follow recommended 'mini.deps' installation |
-
Standalone plugin:
| Branch | Code snippet | |--------|----------------------------------------------------------------| | Main |
add('nvim-mini/mini.diff')| | Stable |add({ source = 'nvim-mini/mini.diff', checkout = 'stable' })|
-
'mini.nvim' library:
| Branch | Code snippet | |--------|-----------------------------------------------| | Main |
{ 'nvim-mini/mini.nvim', version = false },| | Stable |{ 'nvim-mini/mini.nvim', version = '*' },| -
Standalone plugin:
| Branch | Code snippet | |--------|-----------------------------------------------| | Main |
{ 'nvim-mini/mini.diff', version = false },| | Stable |{ 'nvim-mini/mini.diff', version = '*' },|
-
'mini.nvim' library:
| Branch | Code snippet | |--------|------------------------------------------------------| | Main |
