Emacs: diff mode

By Xah Lee. Date: . Last updated: .

emacs diff mode

emacs diff mode 2026-04-15 1d659
emacs diff mode 2026-04-15 1d659
xtodo

Emacs diff mode tutorial (AI generated)

Diff Mode in Emacs is a major mode for viewing, editing, and applying patches (outputs from the diff command or tools like Git). It makes it easy to navigate changes (called "hunks"), refine differences, apply/revert patches selectively, convert between diff formats, and more.

Entering Diff Mode

Diff mode supports unified diffs (most common, from diff -u), context diffs, and to a lesser extent normal diffs.

Basic Structure of a Patch in Diff Mode

A patch contains:

Emacs highlights hunk headers and (by default) refines changes for finer granularity.

Key Navigation Commands

Key Command Description
M-n diff-hunk-next Next hunk (prefix arg for nth)
M-p diff-hunk-prev Previous hunk
M-} diff-file-next Next file (in multi-file patches)
M-{ diff-file-prev Previous file
C-c C-n diff-restrict-view Narrow to current hunk (or file with prefix); C-x n w to widen
C-c C-c diff-goto-source Jump to source file/line (new version by default; prefix for old)

Refining hunks (highlighting word/character-level changes):

Editing and Manipulating Hunks

You can edit the patch like any text buffer. Diff mode automatically updates hunk line numbers (diff-update-on-the-fly; set to nil to disable).

Key Command Description
M-k diff-hunk-kill Kill current hunk
M-K diff-file-kill Kill current file's changes (multi-file)
C-c C-s diff-split-hunk Split hunk at point (unified diffs only)
C-c C-l diff-refresh-hunk Recompute current hunk
C-c C-w diff-ignore-whitespace-hunk Recompute ignoring whitespace (prefix for all)
C-c C-r diff-reverse-direction Reverse patch (old ↔ new); prefix for region

Format conversion:

Applying Patches

Key Command Description
C-c C-a diff-apply-hunk Apply hunk (prefix C-u to reverse/revert)
C-c RET a diff-apply-buffer Apply all hunks, save buffers
C-c C-e diff-ediff-patch Start Ediff session with the patch

Other Useful Commands

Generating Diffs

Customization

Key variables:

Enable in hooks or for specific files: (add-to-list 'auto-mode-alist '("\\.patch\\'" . diff-mode)) or ("\\.diff\\'" . diff-mode).

Tips and Workflows

  1. Quick review in VC: C-x v =M-n/M-p to navigate → C-c C-c to jump to code → C-c C-a to apply/revert hunks selectively.
  2. Selective patching: Split hunks (C-c C-s), kill unwanted ones, then apply.
  3. Ediff integration: Use C-c C-e for interactive side-by-side merging (much more powerful for complex diffs).
  4. Read-only buffers: Many commands work without ESC prefix.
  5. diff-hl-mode (from GNU ELPA): Fringe highlights for changed lines in source buffers + navigation.

Related Tools

For the full details, consult the Emacs manual with C-h i m emacs RET i diff-mode RET or online.

Practice by creating a small patch: Edit two copies of a file, run M-x diff, then experiment with the keys above. Diff mode turns patch handling into a seamless Emacs-native experience!