Automatic diff header recalculation for git add -p hunk editing.
When you edit a hunk during interactive staging (git add -p → e), git requires the @@ -X,Y +A,B @@ header to match the actual line counts. Manual counting is error-prone and leads to "Your edited hunk does not apply" errors. This plugin automatically recalculates the header on save.
{
"jetm/rehunk.nvim",
ft = "diff", -- Load when opening diff files
}use {
"jetm/rehunk.nvim",
ft = "diff",
}- Run
git add -pand select a hunk - Press
eto edit the hunk - Make your changes (add/remove lines)
- Save the file - the header is automatically recalculated
- Close the editor - git applies the corrected hunk
The :RehunkRecalculate command is available in hunk edit buffers if you need to recalculate without saving.
The plugin works out of the box with zero configuration. Optional settings:
{
"jetm/rehunk.nvim",
ft = "diff",
opts = {
auto_recalculate = true, -- Recalculate on save (default: true)
},
}- Neovim 0.8+
- No external dependencies
The plugin detects when Neovim opens git's hunk edit temp file (*addp-hunk-edit.diff). It parses the diff, counts lines by prefix (+ additions, - deletions, context), and updates the header counts:
Y(original count) = context lines + deletion linesB(new count) = context lines + addition lines
Clone the repository and add it to your Neovim config:
-- lazy.nvim local development
{
dir = "~/path/to/rehunk.nvim",
ft = "diff",
}Run tests with:
nvim --headless -u tests/minimal_init.lua -c "PlenaryBustedDirectory tests/ {minimal_init = 'tests/minimal_init.lua'}"Or test core functions in isolation (no Neovim needed):
lua tests/core_spec.lua# Create a test file with changes
echo -e "foo\nbar\nbaz" > /tmp/test.txt
git init /tmp/test-repo && cd /tmp/test-repo
echo -e "foo\nbar\nbaz" > test.txt && git add test.txt && git commit -m "init"
echo -e "1\n2\n3" > test.txt
# Test interactive staging
git add -p
# Select 'e' to edit, modify the hunk, saveMIT