Skip to content

Commit 2cc48ef

Browse files
restrayTimothée Belhomme
authored andcommitted
Revert gitsigns keymaps but fix vimdiff and fugitive conflict
Originally, the keymaps for jumping to next and previous git hunks were ]c and [c. This was changed in nvim-lua#323 (83b65a1) because they overwrote the built-in vimdiff keymaps. However, the more traditional solution is to have ]c and [c *extend* the built-in keymap. This is what fugitive and gitgutter have been doing for years. Gitsigns doesn't do this by itself, but it has a recommended keymap configuration on which the present patch is based: https://github.com/lewis6991/gitsigns.nvim#keymaps The only thing I've added is to have the keymaps work in visual mode as well, which is the same behavior as the built in vimdiff keymaps.
1 parent 5037627 commit 2cc48ef

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

init.lua

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,9 +124,20 @@ require('lazy').setup({
124124
changedelete = { text = '~' },
125125
},
126126
on_attach = function(bufnr)
127-
vim.keymap.set('n', '<leader>gp', require('gitsigns').prev_hunk, { buffer = bufnr, desc = '[G]o to [P]revious Hunk' })
128-
vim.keymap.set('n', '<leader>gn', require('gitsigns').next_hunk, { buffer = bufnr, desc = '[G]o to [N]ext Hunk' })
129-
vim.keymap.set('n', '<leader>ph', require('gitsigns').preview_hunk, { buffer = bufnr, desc = '[P]review [H]unk' })
127+
vim.keymap.set('n', '<leader>hp', require('gitsigns').preview_hunk, { buffer = bufnr, desc = 'Preview git hunk' })
128+
129+
-- don't override the built-in and fugitive keymaps
130+
local gs = package.loaded.gitsigns
131+
vim.keymap.set({'n', 'v'}, ']c', function()
132+
if vim.wo.diff then return ']c' end
133+
vim.schedule(function() gs.next_hunk() end)
134+
return '<Ignore>'
135+
end, {expr=true, buffer = bufnr, desc = "Jump to next hunk"})
136+
vim.keymap.set({'n', 'v'}, '[c', function()
137+
if vim.wo.diff then return '[c' end
138+
vim.schedule(function() gs.prev_hunk() end)
139+
return '<Ignore>'
140+
end, {expr=true, buffer = bufnr, desc = "Jump to previous hunk"})
130141
end,
131142
},
132143
},

0 commit comments

Comments
 (0)