23.9 Comparing Files
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.
After running M-x diff, you can use C-x ` to visit
successive changed locations in the two source files, as in
Compilation mode (see Compilation Mode.) In the ‘*diff*’ buffer,
you can move to a particular hunk of changes and type C-c C-c
(diff-goto-source
) to visit the corresponding source location.
The command M-x diff-backup compares a specified file with its most
recent backup. If you specify the name of a backup file,
diff-backup
compares it with the source file that it is a backup
of.
The command M-x compare-windows compares the text in the
current window with that in the next window. (For more information
about windows in Emacs, Windows.) Comparison starts at point in
each window, after pushing each initial point value on the mark ring
in its respective buffer. Then it moves point forward in each window,
one character at a time, until it reaches characters that don't match.
Then the command exits.
If point in the two windows is followed by non-matching text when
the command starts, it tries heuristically to advance up to matching
text in the two windows, and then exits. So if you use M-x
compare-windows repeatedly, each time it either skips one matching
range or finds the start of another.
With a numeric argument, compare-windows
ignores changes in
whitespace. If the variable compare-ignore-case
is
non-nil
, the comparison ignores differences in case as well.
If the variable compare-ignore-whitespace
is non-nil
,
compare-windows
normally ignores changes in whitespace, and a
prefix argument turns that off.
Differences between versions of files are often distributed as
patches, which are the output from diff or a version
control system that uses diff. M-x diff-mode turns on
Diff mode, a major mode for viewing and editing patches, either as
“unified diffs” or “context diffs.”
You can use M-x smerge-mode to turn on Smerge mode, a minor
mode for editing output from the diff3 program. This is
typically the result of a failed merge from a version control system
“update” outside VC, due to conflicting changes to a file. Smerge
mode provides commands to resolve conflicts by selecting specific
changes.
See also Emerge, and Top, for
convenient facilities for merging two similar files.