Skip to content

Instantly share code, notes, and snippets.

@zdavkeos
Created October 12, 2011 00:21
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save zdavkeos/1279865 to your computer and use it in GitHub Desktop.
Save zdavkeos/1279865 to your computer and use it in GitHub Desktop.
Emacs diff-region
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; diff-region* - Diff two regions
;;
;; To compare two regions, select the first region
;; and run `diff-region`. The region is now copied
;; to a seperate diff-ing buffer. Next, navigate
;; to the next region in question (even in another file).
;; Mark the region and run `diff-region-now`, the diff
;; of the two regions will be displayed by ediff.
;;
;; You can re-select the first region at any time
;; by re-calling `diff-region`.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun diff-region ()
"Select a region to compare"
(interactive)
(when (use-region-p) ; there is a region
(let (buf)
(setq buf (get-buffer-create "*Diff-regionA*"))
(save-current-buffer
(set-buffer buf)
(erase-buffer))
(append-to-buffer buf (region-beginning) (region-end)))
)
(message "Now select other region to compare and run `diff-region-now`")
)
(defun diff-region-now ()
"Compare current region with region already selected by `diff-region`"
(interactive)
(when (use-region-p)
(let (bufa bufb)
(setq bufa (get-buffer-create "*Diff-regionA*"))
(setq bufb (get-buffer-create "*Diff-regionB*"))
(save-current-buffer
(set-buffer bufb)
(erase-buffer))
(append-to-buffer bufb (region-beginning) (region-end))
(ediff-buffers bufa bufb))
)
)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
@zdavkeos
Copy link
Author

I always seem to find myself wanting to diff two regions in Emacs, not two buffers, not to files, not a buffer and a file, just two regions. This pair of functions is intended to mimic WinMerge's Compare/Compare To... functionality. You first select a region with diff-region, which gets saved off in a hidden buffer. You are then free to navigate/edit/work until you have a corresponding region to diff. Running diff-region-now copies the new region to another hidden buffer, and the the diff of the two buffers is displayed by ediff.

This seemed to small to wrap into a full package, but at some point it might be nice. For now, just paste the code into your .emacs file - upon restart/evaluation you will have access to diff-region and diff-region-now.

@vickychijwani
Copy link

Thanks! It works great! You should consider adding this to the Emacs wiki :)

@vifon
Copy link

vifon commented Aug 7, 2012

Great idea but it's already implemented in GNU Emacs: ediff-regions-linewise and ediff-regions-wordwise. Nonetheless, I discovered this thanks to you when I misstyped "diff-region" as "ediff-region". :)

@rashack
Copy link

rashack commented Mar 19, 2014

Very nice, exactly what I needed now!
@vifon I think the ediff-regions-* functions are different, you can't compare regions in the same buffer with them, can you?

@kini
Copy link

kini commented Jun 7, 2014

@rashack: Sure you can. Just select the same buffer in both prompts.

@juboba
Copy link

juboba commented Jun 8, 2022

works great! thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment