When I open a file in Neovim and make changes to it, I'd like to be able to view the difference between the buffer contents and the original file on disk.
Similar to what Sublime Text has:
Of course, a better diff like what wdiff -n $1 $2 | colordiff
gives you would be appreciated but that'd be just a nice to have. Currently, I can't figure out how to get such a diff at all.
My question boils down to:
vnoremap <leader>c something something
for instance)I tried playing around with Diff - Neovim docs but I failed to figure out how to use it - I managed to get the diff once using this sequence of commands in the normal mode:
:command DiffOrig vert new | set buftype=nofile | read ++edit # | 0d_ | diffthis | wincmd p | diffthis
:DiffOrig
... but then it continued to show the same diff and it was very cumbersome to use (splitting the screen repeatedly). It also made some changes to the configuration so when I did :e some/other/file
, it showed weird kind-of-diffy buffer. I didn't inspect this issue too deeply and simply restarted Neovim.
I use Neovim on Xubuntu 24.04, latest Manjaro, and latest Fedora.
I use Neovim with these plugins, if that matters:
Plug 'navarasu/onedark.nvim'
Plug 'junegunn/fzf', { 'do': { -> fzf#install() } }
Plug 'junegunn/fzf.vim'
Plug 'tpope/vim-surround'
Plug 'mg979/vim-visual-multi', {'branch': 'master'}
Thank you very much for any help with this, I'm lost (and LLMs like GPT4-o or Llama weren't too helpful for that matter).
As you can probably see, I'm pretty new to (neo)vim - I used vim only for basic editing before and my daily driver was Sublime Text, until I started working with AsciiDoc which has miserable support in ST. I find Neovim great and wanna learn how to better use it but I miss this "unsaved changes" thing very much.
The :DiffOrig
command you got from the doc is supposed to open a new window beside the current one, with a copy of the file in it, and put both windows in "diff mode". After the split is made, the cursor is brought back to the original
so, from there, you can just keep editing your buffer while the "diff" is updated as you go:
This means that you don't have to do :DiffOrig
for each change to the current buffer. Once is enough.
You can tweak the algorithm via :help 'diffopt'
. Note that I use Vim. Neovim might be different, so YMMV.
Note as well that there are third-party plugins that use the sign column to provide diff information: gitgutter, signify, and probably others. Both offer a similar and rather elegant user experience, but I would be partial to signify because it is not focused on Git only and because I trust the author and maintainer.
FWIW, the "1 line less" message is not very clean. If you decide to keep the :DiffOrig
snippet in your config, I would suggest this little addition:
:command DiffOrig vert new | set buftype=nofile | read ++edit # | silent 0d_ | diffthis | wincmd p | diffthis
^^^^^^
I personally use this custom Git-oriented variant of :DiffOrig
, with the QOL improvement above and the ability to diff with a given revision.