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

How do I stage or undo individual Git hunks without leaving Vim?

Answer

<Leader>hs / <Leader>hu

Explanation

The vim-gitgutter plugin shows Git diff markers in the sign column and provides powerful commands to stage, undo, and preview individual hunks directly from Vim. Instead of switching to a terminal to run git add -p and manually reviewing each hunk, you can visually see changes in real time and act on them with a single keystroke.

How it works

Gitgutter compares your buffer against the Git index and displays +, ~, and - signs in the gutter for added, modified, and removed lines. You can then operate on these hunks:

<Leader>hs    " stage the hunk under the cursor (git add -p equivalent)
<Leader>hu    " undo the hunk under the cursor (revert to index version)
<Leader>hp    " preview the hunk in a floating/popup window

Navigating between hunks

Jump between changed hunks without scrolling:

]c    " jump to the next hunk
[c    " jump to the previous hunk

These mappings let you quickly review every change in a file by hopping from hunk to hunk.

Staging workflow

A typical staging workflow with gitgutter:

  1. Open a file with unstaged changes — signs appear in the gutter
  2. Press ]c to jump to the first hunk
  3. Press <Leader>hp to preview the diff for this hunk
  4. Press <Leader>hs to stage it, or ]c to skip to the next one
  5. Press <Leader>hu on any hunk you want to discard entirely

After staging hunks, the gutter signs update immediately to reflect the new diff state.

Staging partial hunks

You can stage part of a hunk by visually selecting specific lines within the hunk and then pressing <Leader>hs. Only the selected lines are staged, giving you even finer control than git add -p.

Hunk text object

Gitgutter provides a text object ic (inner change) for operating on hunks with any Vim operator:

vic       " visually select the current hunk
omap ic   " use as an operator-pending text object
dic       " delete the current hunk
yic       " yank the current hunk

Undoing hunks safely

The <Leader>hu command reverts the hunk to match the index version. This is a destructive operation on your working tree, but since it only affects the current hunk (not the whole file), it is much safer than git checkout -- file. You can also undo the undo with Vim's u command immediately after.

Folding unchanged lines

Run :GitGutterFold to fold all unchanged lines in the file, leaving only the hunks visible. This gives you a focused view of just the changes, similar to a diff view. Press zr to unfold or :GitGutterFold again to toggle.

Tips

  • Gitgutter updates signs asynchronously and in real time as you edit — set let g:gitgutter_eager = 1 and set updatetime=100 for near-instant feedback
  • Use :GitGutterLineHighlightsToggle to highlight changed lines with background colors instead of just gutter signs
  • Combine with vim-fugitive for a complete Git workflow: use gitgutter for hunk-level operations and fugitive for commits, blame, and branch management
  • For Neovim users, gitsigns.nvim provides the same functionality with Lua-native performance and additional features like inline blame annotations

Next

How do I edit multiple lines at once using multiple cursors in Vim?