ErgoEmacsEmacsLispBlogEmacsLispBuy Tutorial
Web Hosting by 1&1

22.10 Diff Mode

Diff mode is a major mode used for the output of M-x diff and other similar commands, as well as the output of the diff program. 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.

The changes specified in a patch are grouped into hunks, which are contiguous chunks of text that contain one or more changed lines. Hunks can also include unchanged lines to provide context for the changes. Each hunk is preceded by a hunk header, which specifies the old and new line numbers at which the hunk occurs. Diff mode highlights each hunk header, to distinguish it from the actual contents of the hunk.

You can edit a Diff mode buffer like any other buffer. (If it is read-only, you need to make it writable first. See Misc Buffer.) Whenever you change a hunk, Diff mode attempts to automatically correct the line numbers in the hunk headers, to ensure that the diff remains “correct”. To disable automatic line number correction, change the variable diff-update-on-the-fly to nil.

Diff mode treats each hunk as an “error message,” similar to Compilation mode. Thus, you can use commands such as C-x ' to visit the corresponding source locations. See Compilation Mode.

In addition, Diff mode provides the following commands to navigate, manipulate and apply parts of patches:

M-n
Move to the next hunk-start (diff-hunk-next).
M-p
Move to the previous hunk-start (diff-hunk-prev).
M-}
Move to the next file-start, in a multi-file patch (diff-file-next).
M-{
Move to the previous file-start, in a multi-file patch (diff-file-prev).
M-k
Kill the hunk at point (diff-hunk-kill).
M-K
In a multi-file patch, kill the current file part. (diff-file-kill).
C-c C-a
Apply this hunk to its target file (diff-apply-hunk). With a prefix argument of C-u, revert this hunk.
C-c C-b
Highlight the changes of the hunk at point with a finer granularity (diff-refine-hunk). This allows you to see exactly which parts of each changed line were actually changed.
C-c C-c
Go to the source file and line corresponding to this hunk (diff-goto-source).
C-c C-e
Start an Ediff session with the patch (diff-ediff-patch). See Ediff.
C-c C-n
Restrict the view to the current hunk (diff-restrict-view). See Narrowing. With a prefix argument of C-u, restrict the view to the current file of a multiple-file patch. To widen again, use C-x n w (widen).
C-c C-r
Reverse the direction of comparison for the entire buffer (diff-reverse-direction).
C-c C-s
Split the hunk at point (diff-split-hunk). This is for manually editing patches, and only works with the unified diff format produced by the -u or --unified options to the diff program. If you need to split a hunk in the context diff format produced by the -c or --context options to diff, first convert the buffer to the unified diff format with C-c C-u.
C-c C-d
Convert the entire buffer to the context diff format (diff-unified->context). With a prefix argument, convert only the text within the region.
C-c C-u
Convert the entire buffer to unified diff format (diff-context->unified). With a prefix argument, convert unified format to context format. When the mark is active, convert only the text within the region.
C-c C-w
Refine the current hunk so that it disregards changes in whitespace (diff-refine-hunk).
C-x 4 A
Generate a ChangeLog entry, like C-x 4 a does (see Change Log), for each one of the hunks (diff-add-change-log-entries-other-window). This creates a skeleton of the log of changes that you can later fill with the actual descriptions of the changes. C-x 4 a itself in Diff mode operates on behalf of the current hunk's file, but gets the function name from the patch itself. This is useful for making log entries for functions that are deleted by the patch.
M-x diff-show-trailing-whitespaces RET
Highlight trailing whitespace characters, except for those used by the patch syntax (see Useless Whitespace).
blog comments powered by Disqus