16

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).

2
  • ediff-regions-wordwise can work for two regions in the same buffer.
    – abo-abo
    Commented Nov 24, 2015 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 Commented Nov 24, 2015 at 15:23

3 Answers 3

17

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

4
  • 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. Commented Sep 3, 2018 at 9:20
  • emacswiki.org/emacs/CompareRegions also mentions ediff-regions-linewise. Commented Jan 11, 2019 at 13:42
  • 1
    How to actually choose the regions? C-Spc doesn't seem to be it? Commented Sep 15, 2020 at 4: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. Commented Sep 15, 2020 at 5: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).

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.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.