Skip to content

About

Neovim plugin for show diffs on selected code regions. Inspired by ryu1kn/vscode-partial-diff

Topics

Resources

Stars

8 stars

Watchers

1 watching

Forks

Latest commit

Β 

History

39 Commits

Folders and files

Repository files navigation

partial-diff.nvim

License: MIT

A Neovim plugin for performing partial diffs on selected code regions with advanced character-level diff highlighting powered by VSCode's diff algorithm.

Features

  • 🎯 Partial Diff: Compare any two selected regions of code
  • πŸ”€ Character-level highlighting: Powered by VSCode's diff algorithm
  • πŸ”€ Move detection (optional): Recognize moved code blocks instead of showing them as unrelated changes
  • 🧹 Whitespace-aware (optional): Ignore pure indentation / trailing-space changes
  • πŸ“ Histogram fallback: Cleaner built-in diff when the VSCode algorithm is unavailable

Demo

Screen.Recording.2025-10-05.at.16.58.38.mov
partial-diff-demo.mov

Installation

Using lazy.nvim

  {
    "ryutaro-asada/partial-diff.nvim",
  },
use {
  'ryutaro-asada/partial-diff.nvim',
  config = function()
    require('partial-diff').setup()
  end
}

Configuration

require('partial-diff').setup({
  -- Debug options
  debug = false,
  -- Diff computation options
  diff = {
    -- Ignore leading/trailing whitespace differences (vscode-diff).
    -- Set to true to hide pure indentation / trailing-space changes.
    ignore_trim_whitespace = false,
    -- Detect moved code blocks (vscode-diff). When enabled, moved blocks are
    -- highlighted with the `line_move` group and marked with virtual text
    -- pointing to their counterpart location, instead of being shown as a
    -- change.
    compute_moves = false,
    -- Align corresponding lines between From and To by inserting filler
    -- rows (like diff mode), so both windows scroll in lockstep. The number
    -- column shows original line numbers (blank on filler rows).
    align = true,
    -- Keep the From/To windows scroll- and cursor-synced (like diff mode).
    -- Applies to the vscode-diff path; the built-in diff fallback already
    -- syncs scrolling on its own.
    sync_scroll = true,
    -- When sync_scroll is on, also turn off 'wrap' in both windows so that
    -- horizontal scrolling actually happens (and stays mirrored). Set to
    -- false to keep wrap at the cost of no horizontal sync.
    sync_scroll_nowrap = true,
    -- diffopt used for the built-in Neovim diff fallback (when Node.js /
    -- vscode-diff are unavailable). Defaults to the histogram algorithm with
    -- linematch for cleaner results on refactored code.
    fallback_diffopt =
      "internal,filler,closeoff,hiddenoff,algorithm:histogram,linematch:60,context:3,indent-heuristic",
  },
  -- Highlight customization
  highlights = {
    -- Link to existing highlight groups
    line_change = 'DiffChange',
    line_add = 'DiffAdd',
    line_delete = 'DiffDelete',
    -- Specify custom colors
    char_add = {
      bg = '#2d5a2d',   -- Background color
      fg = '#a9dc76',   -- Foreground color
      bold = true,      -- Bold
      italic = false,   -- Italic
      underline = false -- Underline
    },
    char_delete = {
      bg = '#5a2d2d',
      fg = '#ff6188',
    },
    char_change = {
      bg = '#4a4a0e',
      fg = '#ffd866',
      bold = true
    },
    -- Highlight for moved blocks (used when diff.compute_moves = true)
    line_move = {
      bg = '#2d3a5a',
      fg = '#78a9ff',
    },
    -- Highlight for filler rows inserted by diff.align (links to DiffDelete
    -- by default, like diff mode)
    filler = 'DiffDelete',
  }
})

VSCode Diff Algorithm (Optional but Recommended)

For better character-level diff highlighting, install the VSCode diff algorithm:

  1. Ensure Node.js is installed on your system
  2. Run :PartialDiffInstallVSCode in Neovim
  3. This will install the required npm packages in the plugin directory

Without this, the plugin will fall back to Neovim's built-in diff.

Commands

Primary Commands

  • :PartialDiffFrom - Mark a range of lines as the source (what you're diffing from)
  • :PartialDiffTo - Mark a range of lines as the target (what you're diffing to) and display the diff
  • :PartialDiffDelete - Close the current diff view
  • :PartialDiffInstallVSCode - Install VSCode diff algorithm dependencies

Legacy Commands (Backward Compatibility)

  • :PartialDiffA - Alias for :PartialDiffFrom
  • :PartialDiffB - Alias for :PartialDiffTo

Debug Commands

  • :PartialDiffShowLog - Show the debug log file
  • :PartialDiffClearLog - Clear the debug log file

Usage

Basic Workflow

  1. Select the first range of lines in visual mode
  2. Execute :PartialDiffFrom (or use your keymap)
  3. Select the second range of lines in visual mode
  4. Execute :PartialDiffTo (or use your keymap)
  5. View the diff in a new tab with side-by-side comparison

Example

" Select lines 10-20 in visual mode
:'<,'>PartialDiffFrom

" Select lines 30-40 in visual mode
:'<,'>PartialDiffTo

" Close the diff when done
:PartialDiffDelete

Using

  • vscode-diff - Advanced character-level diff computation

Requirements

  • Neovim 0.10.0 or later
  • (Optional) Node.js for VSCode diff algorithm

Contributing

If you have any suggestions or improvements, feel free to open an issue or submit a pull request.

Running the tests

The test suite lives in spec/ and runs in headless Neovim with plenary.nvim. Node.js and npm are needed for the vscode-diff tests.

make test

make test fetches a pinned plenary.nvim into .deps/ and installs the npm dependencies on first run. The manual fixtures in test/ are reused as test inputs.

License

This project is licensed under the MIT License.

About

Neovim plugin for show diffs on selected code regions. Inspired by ryu1kn/vscode-partial-diff

Topics

Resources

Stars

8 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages