Skip to content

Instantly share code, notes, and snippets.

@zdavkeos
Created October 12, 2011 00:21
Show Gist options
  • 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))
)
)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
@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