A lightweight Neovim plugin for applying LLM-generated patches with fuzzy matching.
- Apply simple diff patches from buffer to target file
- Fuzzy matching with whitespace tolerance
- Handles interleaved additions/removals
- Forgiving format: accepts patches with or without space-prefixed context lines
- Preview mode for dry-run validation
{
'kyleking/patch_it.nvim',
config = function()
require('patch_it').setup()
end,
}MiniDeps.add('kyleking/patch_it.nvim')
require('patch_it').setup()use {
'kyleking/patch_it.nvim',
config = function()
require('patch_it').setup()
end,
}Plug 'kyleking/patch_it.nvim'
" In your init.lua or after/plugin:
lua require('patch_it').setup()require('patch_it').setup({
fuzzy_lines = 50, -- Reserved for future hint-based search
whitespace_trim = true, -- Normalize whitespace when matching (default: true)
}):PatchApply path/to/target.luaApplies the current buffer as a patch to the specified target file.
local patch_it = require('patch_it')
-- Apply patch string to target file
patch_it.apply(patch_string, target_path)
-- Apply patch string with preview (dry-run)
patch_it.apply(patch_string, target_path, { preview = true })
-- Apply current buffer as patch to target file
patch_it.apply_buffer(target_path)
-- Preview what would be applied
patch_it.apply_buffer(target_path, { preview = true })- LLM outputs a patch in chat/response
- Yank patch into a scratch buffer
- Run
:PatchApply path/to/target.lua - Plugin fuzzy-matches context, applies changes
- If wrong, hit
uto undo
The plugin accepts patches in a simple diff format (as commonly produced by LLMs like CodeRabbit):
+import { useMemo } from 'react';
import { dark } from '@clerk/themes';
-import { useTheme } from 'src/common/ThemeToggle';
+import { useTheme } from '#common/ThemeToggle.js';+content- Line to add (no space after +)-content- Line to remove (no space after -)content- Context line (single space prefix)content- Context line without prefix (forgiving mode)- Empty lines are treated as context
--- a/fileand+++ b/fileheaders are warned and ignored@@ ... @@hunk headers are skipped
- v1 supports single hunk, single file
- Files with lines starting with literal
+or-(e.g., markdown lists) need space-prefixed context in the patch
The apply() function returns a result table:
-- Success
{
success = true,
buf = <buffer_number>,
start_line = 10, -- 1-indexed
lines_removed = 3,
lines_added = 5,
}
-- Preview success
{
success = true,
preview = true,
buf = <buffer_number>,
match_line = 10,
original = { ... }, -- Lines that would be removed
replacement = { ... }, -- Lines that would be added
}
-- Failure
{
success = false,
error = "No match found for original lines",
}nvim --headless -u NONE --cmd "set rtp+=." -l tests/patch_it_spec.luaMIT