How do I see a diff of my unsaved changes compared to the version on disk?
Answer
:w !diff % -
Explanation
The command :w !diff % - pipes the current buffer's contents to an external diff command that compares it against the saved file on disk. This lets you review exactly what you have changed since the last save — without needing to write to a temp file or open a new split.
How it works
:w !cmd— writes the buffer contents to standard input ofcmdrather than to diskdiff % -— runsdiffwith two arguments:%expands to the current file's path (the on-disk version), and-reads from standard input (the unsaved buffer)- The output is a standard unified diff showing added and removed lines
Example
You open config.lua, change timeout = 300 to timeout = 600, and add a new line. Before saving:
:w !diff % -
Output:
3c3
< timeout = 300
---
> timeout = 600
5a6
> new_option = true
Tips
- For a more readable unified diff with context lines:
:w !diff -u % - - Add it as a command mapping in your vimrc so it is always at hand:
command! DiffSaved w !diff -u % - - If
diffis not available, usegit difffor files inside a git repo::!git diff % - Neovim users can also use
vim.diff()via Lua, but:w !diff % -works in both Vim and Neovim without configuration - This is especially useful in CI-like environments or when reviewing changes before a
:wq