vimtricks.wiki Concise Vim tricks, one at a time.

How do I jump between git diff hunks in the current buffer and stage or preview them with gitsigns.nvim?

Answer

]c / [c (gitsigns.nvim)

Explanation

gitsigns.nvim decorates your buffer with inline git change indicators and provides motions to jump directly between diff hunks without opening a separate diff view. Combined with hunk staging and preview actions, you can review and stage individual changes without ever leaving the file you are working in.

How it works

After installing gitsigns.nvim and setting up the recommended keymaps, the following motions and actions are available:

Navigation:

  • ]c — jump to the next hunk (added, changed, or deleted lines)
  • [c — jump to the previous hunk

Hunk actions (example mappings from the README):

  • <leader>hs — stage the hunk under the cursor
  • <leader>hr — reset (undo) the hunk
  • <leader>hp — preview the hunk in a floating window
  • <leader>hb — show git blame for the current line inline

The ]c / [c motions accept a count: 3]c jumps forward three hunks.

Example

You have modified several functions in a large file. Press ]c to jump to the first changed hunk. Use <leader>hp to preview the diff in context. If the change looks good, press <leader>hs to stage just that hunk. Continue with ]c to move to the next hunk.

Tips

  • ]c and [c are the gitsigns-provided versions; the vanilla Vim ]c / [c only work in :diffmode. gitsigns overrides them when vim.wo.diff is false
  • Use <leader>hS to stage the entire buffer at once
  • <leader>tb toggles current-line blame (inline virtual text showing author/date)
  • Run :Gitsigns toggle_signs to show or hide the sign column decorations

Next

How do I incrementally expand or shrink a selection based on syntax tree nodes using nvim-treesitter?