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

How do I open a diff between my current buffer and the last saved version of the file?

Answer

:DiffOrig

Explanation

:DiffOrig opens a side-by-side split comparing your current (unsaved) buffer against the version on disk. It uses Vim's built-in diff mode so you can review every change you've made since the last save before committing, writing, or discarding.

How it works

:DiffOrig is defined in Vim's defaults.vim and Neovim's built-in defaults. It:

  1. Opens a scratch buffer and reads in the saved file contents
  2. Enables diff mode (diffthis) on both windows
  3. Jumps to the first difference automatically

The scratch buffer is read-only and not backed by a file — it exists only for the comparison.

Example

You've edited several functions but haven't saved. Run:

:DiffOrig

A split opens with your live edits on one side and the saved file on the other, with changes highlighted. Navigate hunks with ]c (next change) and [c (previous change).

To close:

:diffoff

Then <C-w>c to close the scratch window.

Tips

  • dp (diff put) pushes the current hunk to the other window; do (diff obtain) pulls from it
  • If :DiffOrig is unavailable in older Vim, add to your vimrc:
    command DiffOrig vert new | set bt=nofile | r ++edit # | 0d_ | diffthis | wincmd p | diffthis
    
  • This is a quick pre-save review workflow — complementary to :w !diff % - which uses external diff

Next

How do I comment out all lines matching a pattern at once using vim-commentary?