function on_attach(bufnr) local gs = package.loaded.gitsigns local function map(mode, l, r, opts) opts = opts or {} opts.buffer = bufnr vim.keymap.set(mode, l, r, opts) end -- Navigation map("n", "]h", function() if vim.wo.diff then return "]h" end vim.schedule(function() gs.next_hunk() end) return "" end, { expr = true }) map("n", "[h", function() if vim.wo.diff then return "[h" end vim.schedule(function() gs.prev_hunk() end) return "" end, { expr = true }) -- Actions map({ "n", "v" }, "gs", ":Gitsigns stage_hunk") map({ "n", "v" }, "gr", ":Gitsigns reset_hunk") map("n", "gu", gs.undo_stage_hunk) map("n", "gp", gs.preview_hunk) map("n", "gb", function() gs.blame_line({ full = true }) end) map("n", "gtb", gs.toggle_current_line_blame) map("n", "gd", gs.diffthis) map("n", "gD", function() gs.diffthis("~") end) -- Text object map({ "o", "x" }, "ih", ":Gitsigns select_hunk") end -- See: https://github.com/lewis6991/gitsigns.nvim require("gitsigns").setup({ on_attach = on_attach, })