Neovim has a builtin diff mode. To start the diff mode, simply open a
split view and open one file in each buffer and run the :diffthis
command in both buffers.
For example, we can use diff mode to compare the local working version
of the file with the version in HEAD like this:
-
1. Open a new scratch buffer
:vnew
.
-
2. Open the old version of the file
:r !git show HEAD:<filename>
.
-
3. Execute
:diffthis
in both buffers.
We can now see the changes highlighted side by side. However, it’s not
ideal to manually open a split view and load the file content using a
Git command every time. That’s where diffview.nvim comes in.
Diffview.nvim uses Neovim’s built-in diffing capabilities, but what
makes it really powerful is how it simplifies working with Git inside
Neovim. To archive the same result of what we just did we can simply
run :DiffviewOpen
Another really usefull command is :DiffviewFileHistory %
. This lets you browse all commits that affected the currently open
file and review the changes introduced in each.
Another useful command is :'<,'>DiffviewFileHistory
, which shows only the commits that affected the current visual
selection.