Given a modified buffer, how can I diff it with the file backing it on disk to see what I've changed?
- Related/Duplicate: How to view diff when emacs suggests to recover this file?Kaushal Modi– Kaushal Modi2015-01-17 17:11:48 +00:00Commented Jan 17, 2015 at 17:11
- There has been the same question at unix.stackexchange.com, but with less asnwers.imz -- Ivan Zakharyaschev– imz -- Ivan Zakharyaschev2015-01-24 10:00:06 +00:00Commented Jan 24, 2015 at 10:00
3 Answers
You want the command M-x diff-buffer-with-file. See the manual:
diff-buffer-with-file is an interactive autoloaded compiled Lisp function in `diff.el'.
(diff-buffer-with-file &optional BUFFER)View the differences between BUFFER and its associated file. This requires the external program
diffto be in yourexec-path.
You may also be interested in highlight-changes-mode which automatically and interactively highlights changes made to the buffer after it was enabled.
- Along the lines of
highlight-changes-modethere's also thediff-hlpackage in GNU ELPA.Stefan– Stefan2015-01-17 04:00:24 +00:00Commented Jan 17, 2015 at 4:00 - 1Yes, but right now it only works to show diffs between the (saved) buffer and a
vcrevision. I worked on diffing the buffer (as opposed to its backing file) in diff-hl #33, but it's blocked on a bug in the Emacs core.PythonNut– PythonNut2015-01-17 05:27:07 +00:00Commented Jan 17, 2015 at 5:27 - Do you have a bug-report number for that?Stefan– Stefan2015-01-17 14:01:57 +00:00Commented Jan 17, 2015 at 14:01
- @Stefan, I haven't gotten around to it yet.PythonNut– PythonNut2015-01-18 05:23:59 +00:00Commented Jan 18, 2015 at 5:23
- 1Got fixed a (probably) long time ago: works in 2019! :)mistige– mistige2019-07-27 10:26:09 +00:00Commented Jul 27, 2019 at 10:26
Command ediff-current-file:
ediff-current-file is an interactive autoloaded Lisp function in `ediff.el'. (ediff-current-file) Start ediff between current buffer and its file on disk. This command can be used instead of `revert-buffer'. If there is nothing to revert then this command fails. - 5
ediff-current-filehas some benefits overdiff-buffer-with-fileif you want interactive review of changes rather than a plain diff. The interactive review even allows to selectively revert some parts, in case you finally don't want to save them. See also emacs.stackexchange.com/a/3778/10614 for a more complete answer.Stéphane Gourichon– Stéphane Gourichon2015-12-29 11:20:52 +00:00Commented Dec 29, 2015 at 11:20
A simple way to achieve this is with the package line-reminder, which shows unsaved and saved changes in current session. You can enable it globally with
(global-line-reminder-mode t) or on a per-buffer basis with
(line-reminder-mode t)