Weekly Emacs tip #28 — Examine the differences between a buffer and its file
Sometimes I want to see the differences between a file (i.e. the thing on disk) and the Emacs buffer that “visits” the file. For example, before saving a file, I might want to check what changes will be saved to disk.
Another thing that happens to me regularly is that a file gets changed on disk, mostly because it is synced via a cloud service like Nextcloud. In this latter case Emacs won’t allow you to just edit the file. Instead, it will tell you that the file on disk has changed and whether you want to reload the file before editing it.
In these cases, you can use the command diff-buffer-with-file
to investigate the changes. The command opens a new buffer named *Diff*
(in a new window) that shows the output of the GNU diff
tool. Neat and simple!
What may also come in handy is that if you keep the *Diff*
buffer open and press the g
key to update the diff (side note: g
is often used as a refresh or update key in Emacs).
No Comments