diff options
| author | Thomas Vanbesien <tvanbesi@proton.me> | 2026-06-29 17:45:01 +0200 |
|---|---|---|
| committer | Thomas Vanbesien <tvanbesi@proton.me> | 2026-06-30 14:56:54 +0200 |
| commit | 81908b806008a002a322dd7f51cea2068150e9ea (patch) | |
| tree | 4b3bb2ec7f581de17b97177eeef2b9f264abfdc9 /plugin | |
| parent | 6a2a43c97810e2288700d24b2b9cd9e83449b73b (diff) | |
| download | nvim-config-81908b806008a002a322dd7f51cea2068150e9ea.tar.gz nvim-config-81908b806008a002a322dd7f51cea2068150e9ea.zip | |
feat(git): replace vim-gitgutter with gitsigns.nvim
Swap the diff-sign plugin for gitsigns.
Point lualine's diff component at gitsigns' status dict instead of
letting it spawn its own git diff.
Diffstat (limited to 'plugin')
| -rw-r--r-- | plugin/00-plugin.lua | 2 | ||||
| -rw-r--r-- | plugin/50-color.lua | 9 | ||||
| -rw-r--r-- | plugin/50-gitsigns.lua | 67 |
3 files changed, 77 insertions, 1 deletions
diff --git a/plugin/00-plugin.lua b/plugin/00-plugin.lua index ac34066..6812ced 100644 --- a/plugin/00-plugin.lua +++ b/plugin/00-plugin.lua @@ -5,9 +5,9 @@ require() them. ]] vim.pack.add({ - "https://github.com/airblade/vim-gitgutter", "https://github.com/ibhagwan/fzf-lua", "https://github.com/jbyuki/one-small-step-for-vimkind", + "https://github.com/lewis6991/gitsigns.nvim", "https://github.com/maxmx03/solarized.nvim", "https://github.com/MeanderingProgrammer/render-markdown.nvim", "https://github.com/mfussenegger/nvim-dap", diff --git a/plugin/50-color.lua b/plugin/50-color.lua index f95711c..4cdbad0 100644 --- a/plugin/50-color.lua +++ b/plugin/50-color.lua @@ -100,7 +100,16 @@ local function bold_diff_diag() end end +-- Diff counts for lualine's diff component, sourced from gitsigns' status dict. +local function gitsigns_diff() + local gs = vim.b.gitsigns_status_dict + if gs then + return { added = gs.added, modified = gs.changed, removed = gs.removed } + end +end + local lualine_sections = { + lualine_b = { "branch", { "diff", source = gitsigns_diff }, "diagnostics" }, lualine_c = { statuses }, lualine_x = { "encoding", "fileformat", "filetype", "lsp_status" }, } diff --git a/plugin/50-gitsigns.lua b/plugin/50-gitsigns.lua new file mode 100644 index 0000000..88337b8 --- /dev/null +++ b/plugin/50-gitsigns.lua @@ -0,0 +1,67 @@ +--[[ 50-gitsigns.lua — git diff signs plus hunk navigation/staging, via gitsigns.nvim. + +Signs render by default in the always-on sign column; changed lines, their line numbers, +and the changed words within them are highlighted too (linehl/numhl/word_diff). + +Keymaps are buffer-local (set in on_attach): +`]c`/`[c` navigate hunks (falling through to Vim's builtin diff-mode nav when 'diff' is set) +`<Leader>h…` stage/reset/preview/blame, and `ih` is a hunk text object. + +lualine reads the diff counts from gitsigns (see 50-color.lua). +]] + +local gitsigns = require("gitsigns") + +-- Jump to the next/prev hunk, falling through to builtin ]c/[c when in diff mode. +local function nav_hunk(direction) + if vim.wo.diff then + vim.cmd.normal({ direction == "next" and "]c" or "[c", bang = true }) + else + gitsigns.nav_hunk(direction) + end +end + +-- Stage/reset the visually-selected range of lines. +local function stage_selection() + gitsigns.stage_hunk({ vim.fn.line("."), vim.fn.line("v") }) +end +local function reset_selection() + gitsigns.reset_hunk({ vim.fn.line("."), vim.fn.line("v") }) +end + +local function blame_line() + gitsigns.blame_line({ full = true }) +end + +-- Buffer-local gitsigns keymaps; passed as `on_attach`. +local function set_keymaps(bufnr) + local function map(mode, lhs, rhs, desc) + vim.keymap.set(mode, lhs, rhs, { buffer = bufnr, desc = desc }) + end + + -- Hunk navigation. + map("n", "]c", function() + nav_hunk("next") + end, "Next git hunk") + map("n", "[c", function() + nav_hunk("prev") + end, "Previous git hunk") + + -- Staging / resetting. `stage_hunk` toggles, so it also unstages a staged hunk. + map("n", "<Leader>hs", gitsigns.stage_hunk, "Stage/unstage hunk") + map("n", "<Leader>hr", gitsigns.reset_hunk, "Reset hunk") + map("x", "<Leader>hs", stage_selection, "Stage selected hunk") + map("x", "<Leader>hr", reset_selection, "Reset selected hunk") + map("n", "<Leader>hS", gitsigns.stage_buffer, "Stage buffer") + map("n", "<Leader>hR", gitsigns.reset_buffer, "Reset buffer") + + -- Inspecting. + map("n", "<Leader>hp", gitsigns.preview_hunk, "Preview hunk") + map("n", "<Leader>hb", blame_line, "Blame line") + map("n", "<Leader>hd", gitsigns.diffthis, "Diff against index") + + -- Hunk text object. + map({ "o", "x" }, "ih", gitsigns.select_hunk, "Inner git hunk") +end + +gitsigns.setup({ on_attach = set_keymaps, linehl = true, numhl = true, word_diff = true }) |
