Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Command to show difference between a buffer and its corresponding file

Tags:

emacs

diff

Is there an emacs command to show the difference between a buffer and its file?

For example, I've made some edits to a buffer, forgotten what they were, and now I need to save the buffer to its file, but want to check what I have actually changed first.

like image 723
bph Avatar asked Feb 16 '12 15:02

bph


People also ask

What is the difference between file and buffer?

A buffer is something in memory, a file resides on a hard disk.

What is the command to write buffer to a specified file?

If you wish to mark the buffer as visiting a different file and save it right away, use C-x C-w ( write-file ). This is equivalent to set-visited-file-name followed by C-x C-s , except that C-x C-w asks for confirmation if the file exists.

How do I diff files in Emacs?

The command M-x diff compares two files, displaying the differences in an Emacs buffer named `*diff*' . It works by running the diff program, using options taken from the variable diff-switches . The value of diff-switches should be a string; the default is "-c" to specify a context diff.

What is diff mode?

Diff mode is a major mode used for the output of M-x diff and other similar commands. This kind of output is called a patch, because it can be passed to the patch command to automatically apply the specified changes. To select Diff mode manually, type M-x diff-mode .


2 Answers

diff-buffer-with-file. I just googled for "emacs compare buffer file changed".

like image 55
choroba Avatar answered Oct 19 '22 16:10

choroba


Another option is to hit C-x s followed by d. This also shows a diff between a buffer and its file.

Most useful if the buffer of interest is the only one that needs saving.

like image 42
bph Avatar answered Oct 19 '22 16:10

bph