While vim-fugitive
does not supply direct analogues for git diff --staged
or git diff --cached
, it does supply a general-purpose Vim command for piping the output of arbitrary git
commands into read-only Vim buffers: :Git!
.
Inapplicable Answers
Before we get to that, let's explicitly restate the question. git diff --staged
and git diff --cached
are synonyms for the same underlying operation: diffing the contents of the index (the set of all staged changes) against the contents of the HEAD (the most recent commit for the current branch), typically for reviewing changes prior to commit. The stated question then becomes:
What is the most effective means of reviewing all staged changes in vim-fugitive
?
It should be clear that the currently accepted self-answer fails to address this question. The next highest rated self-answer is no better.
:Gstatus
bindings only apply to the file on the current line and hence cannot by definition be used to review all staged changes. Moreover, the :Gstatus
D
binding doesn't even review all staged changes for the file on the current line. It only diffs the index and working tree copies of that file, rather than diffing the index and most recently committed copies of that file (which is an altogether different beast).
:Gdiff HEAD
is similarly inapplicable. It only diffs the most recently committed and working tree copies of the file corresponding to the current buffer. :Gdiff
without an argument is equivalent to the :Gstatus
D
binding, again diffing the index and working tree copies of that file. Neither reviews all staged changes.
Applicable Answers
emaniacs struck the closest to a working solution with this comment to the latter answer:
:Git diff --staged
Now we're approximating the truth!
:Git
pipes the output of the passed git
command to the current external pager, permitting a liesurely review of all staged changes external to Vim. But there's the rub: external to Vim. That means no Vim bindings, buffers, or syntax highlighting. Ideally, we'd prefer a read-only Vim buffer syntax highlighting the output of git diff --staged
. Can we do this?
The Solution
We can, or Tim Pope isn't the Vim Pope. The !
-suffixed variant of :Git
does just that, permitting a liesurely review of all staged changes within Vim complete with Vim-based syntax highlighting of change differences:
:Git! diff --staged
Yeah. It's pretty awesomeness.
But let's go a step farther. In the time-honoured tradition of slothful slackers everywhere, let's define a new Vim command :Greview
encapsulating this operation and a new binding <leader>gr
running this command. Just stash the following into your .vimrc
:
command Greview :Git! diff --staged
nnoremap <leader>gr :Greview<cr>
Assuming <leader>
to be ,
, reviewing all staged changes reduces to ,gr
. It couldn't get any Vimmier.