15

ediff allows comparing files, buffers, and regions in different buffers. However, it does not let one compare sections in the same buffer.

Until now I used isearch for that: C-s at the beginning of the 1st identical section, the beginning of the second section is highlighted, then C-w to increase the search string until the second section no longer match.

Today I got Too many words error after several C-w.

So, what do people use these days to compare sections of the same buffer?

(yes, I can copy one of the sections into a throwaway buffer, but this is ugly).

Drew
  • 75,699
  • 9
  • 109
  • 225
sds
  • 5,928
  • 20
  • 39
  • ediff-regions-wordwise can work for two regions in the same buffer. – abo-abo Nov 24 '15 at 14:53
  • 2
    You could also use `clone-indirect-buffer-other-window` (C-x 4 c) and then narrow the two buffers (C-x n n) and use `ediff` on the two narrowed buffers – fredtantini Nov 24 '15 at 15:23

3 Answers3

16

You can use ediff-regions-wordwise. It prompts you for two buffers, and then for two regions. The buffers can be the same.

abo-abo
  • 13,943
  • 1
  • 29
  • 43
  • 3
    This is simple to use (emacs provides guidance) and works. If your environment catches `C-M-c` key combination, then you can us the old trick of replacing Meta with a previous Escape. This becomes : `Esc C-c`. – Stéphane Gourichon Sep 03 '18 at 09:20
  • https://www.emacswiki.org/emacs/CompareRegions also mentions `ediff-regions-linewise`. – Stéphane Gourichon Jan 11 '19 at 13:42
  • 1
    How to actually choose the regions? C-Spc doesn't seem to be it? – ShreevatsaR Sep 15 '20 at 04:23
  • Ah there's a box that's supposed to pop up, saying something like "hit C-M-spc when done" (I forgot the exact key now), but because of some bug in Aquamacs(?) wasn't showing up properly. – ShreevatsaR Sep 15 '20 at 05:37
2

I wrote smerge-makeup-conflict for similar uses. Admittedly, it's not polished but I find it does the job (when you can modify the buffer):

  • C-SPC at the beginning of the first part,
  • C-SPC at the beginning of the second (which should be after the end of the first, and ideally close to it),
  • then move to the end of the second part and do M-x smerge-makeup-conflict.

At that point you're in smerge-mode and you can use its functionality to compare (I typically mostly rely on smerge-refine).

Stefan
  • 26,154
  • 3
  • 46
  • 84
1

One thing I sometimes use for this is command compare-windows (repeated). It compares the text in two windows, starting at point.

Compare text in current window with text in next window. Compares the text starting at point in each window, moving over text in each one as far as they match.

This command pushes the mark in each window at the prior location of point in that window. If both windows display the same buffer, the mark is pushed twice in that buffer: first in the other window, then in the selected window.

A prefix arg means reverse the value of variable compare-ignore-whitespace. If compare-ignore-whitespace is nil, then a prefix arg means ignore changes in whitespace. If compare-ignore-whitespace is non-nil, then a prefix arg means don't ignore changes in whitespace. The variable compare-windows-whitespace controls how whitespace is skipped. If compare-ignore-case is non-nil, changes in case are also ignored.

If compare-windows-sync is non-nil, then successive calls of this command work in interlaced mode: on first call it advances points to the next difference, on second call it synchronizes points by skipping the difference, on third call it again advances points to the next difference and so on.

(Another thing you can do is, as you said, to create another, throwaway, buffer, copy the content, and compare the two buffers. Or use an indirect buffer, as @fredtantini suggested.)

Drew
  • 75,699
  • 9
  • 109
  • 225