Skip to content

[idea] Show git diff hunk stats in statusline via gitsigns_status_dict #48

Description

@stanfish06

What

Add +N ~N -N hunk counts to the statusline's git section, sourced from vim.b.gitsigns_status_dict — the same buffer-local variable that already powers the branch name display.

Where

lua/config/statusline.lua:27-33 — the current_git_branch() function.

Why it matters

The statusline already reads vim.b.gitsigns_head for the branch name. gitsigns also populates vim.b.gitsigns_status_dict with { added = N, changed = N, removed = N } for free — no extra API calls. Adding the counts gives a quick at-a-glance diff summary without opening fugitive or running :Gstatus.

This is especially useful during the iterative edit-format-commit cycle across the six active LSP languages (Lua, Python, TypeScript, Rust, Go, Swift), where knowing "I have 5 additions and 2 changes in this buffer" is a constant need.

Sketch

local function current_git_branch()
    local ok, gs = pcall(require, "gitsigns")
    if not ok then return "" end
    local branch = vim.b.gitsigns_head
    if not branch or branch == "" then return "" end

    local stats = vim.b.gitsigns_status_dict
    local diff = ""
    if stats then
        local added   = stats.added   or 0
        local changed = stats.changed or 0
        local removed = stats.removed or 0
        if added > 0 or changed > 0 or removed > 0 then
            diff = string.format(" +%d ~%d -%d", added, changed, removed)
        end
    end

    return " %#Git#  " .. branch .. diff .. " " .. "%*"
end

The diff string is omitted when all counts are zero (clean buffer), keeping the statusline tidy for unmodified files.

Notes

  • Requires gitsigns to be set up (see issue [idea] configure gitsigns.nvim keymaps for hunk staging and blame #14). Once that's done, vim.b.gitsigns_status_dict is populated automatically.
  • Colors can be split per-segment (green +, yellow ~, red -) using additional highlight groups if desired, but a single %#Git# block is the simplest starting point.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions