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

How do I preview, stage, and navigate Git hunks inline with gitsigns?

Answer

:Gitsigns preview_hunk

Explanation

gitsigns.nvim shows Git diff indicators in the sign column and provides hunk-level operations — preview, stage, reset, and navigate — all without leaving Vim. It's faster than fugitive for quick inline Git operations.

How it works

  • :Gitsigns preview_hunk — show the hunk diff in a floating window
  • :Gitsigns stage_hunk — stage the current hunk
  • :Gitsigns reset_hunk — discard changes in the current hunk
  • :Gitsigns next_hunk / prev_hunk — navigate between changed sections
  • Signs appear automatically: + (added), ~ (modified), - (deleted)

Example

" Recommended keybindings
nnoremap ]c :Gitsigns next_hunk<CR>
nnoremap [c :Gitsigns prev_hunk<CR>
nnoremap <leader>hp :Gitsigns preview_hunk<CR>
nnoremap <leader>hs :Gitsigns stage_hunk<CR>
nnoremap <leader>hr :Gitsigns reset_hunk<CR>
nnoremap <leader>hb :Gitsigns blame_line<CR>
Sign column shows:
  │+ added line
  │~ modified line
  │- deleted line

<leader>hp shows a floating diff preview
<leader>hs stages just that hunk

Tips

  • blame_line shows inline Git blame for the current line
  • Visual mode: select lines then :Gitsigns stage_hunk to stage partial hunks
  • :Gitsigns diffthis opens a diff of the current file vs index
  • Performance: gitsigns is async and handles large repos efficiently

Next

How do I return to normal mode from absolutely any mode in Vim?