37

One feature that Xcode has which I think is pretty neat is a "Comparison" mode, which continually shows differences between the current buffer and the most recent commit.

Comparison mode

Is there anything like this for Emacs? I don't really care about the simultaneous side-by-side views. Just highlighting the background of any lines that were changed would be a great help.

I know I can always do C-x v =, but it's a static view of one point in time, and only shows the diffs. I want to see my entire file, editable, with the diffs highlighted.

Ken
  • 371
  • 3
  • 3

3 Answers3

33

Dmitry Gutov's diff-hl library provides diff-hl-mode, which gives you the fringe highlighting shown in the top window in the following screenshot (with the actual diff displayed in the bottom window for comparison):

diff-hl screenshot

It uses the generic vc functionality in Emacs in order to be VCS-agnostic. The readme says "Tested with Git, Mercurial, Bazaar and SVN. May work with other VC backends, too."

Note that diff-hl-margin-mode can be employed if you're using terminal Emacs rather than GUI Emacs.

By default diff-hl only highlights the differences between the saved file and the repository; but diff-hl-flydiff-mode enables un-saved differences to be highlighted as well, so that you can see the changes while you edit.

Also note the built-in (to standard Emacs) highlight-changes-mode. That doesn't really tackle your problem out-of-the-box, but the highlight-compare-* functions might facilitate a solution.

phils
  • 50,977
  • 3
  • 79
  • 122
  • #33 has been resolved. – Dmitry Sep 30 '15 at 01:16
  • 1
    Dmitry: Indeed -- I've actually been trying it out over the past few days, and I can confirm that this feature works very well :) – phils Sep 30 '15 at 01:30
  • I'm glad to hear it; it was written by one very patient contributor. Thanks for updating the answer! – Dmitry Sep 30 '15 at 02:47
  • 1
    Oh wowzers thank you @Dmitry, that's a gem of a diff package! For everyone else here, confirm that the package is still maintained by Dmitry for the 9 years since the answer has been posted, last commit 3 weeks ago as of March '24. As for the tramp warning in the README,, maybe it sometimes slow, but perhaps nothing of significance with the modern 100Mbps+ speeds. I am an active tramp user (working w/ cloud VMs), and see no issues w/ this package. – kkm -still wary of SE promises Mar 20 '24 at 01:09
11

You don't specify what version control system you're using, but if it's git, ou can use git-gutter to put an indicator in the margins when a line has been added, deleted or modified. It's available in MELPA via M-x package-install git-gutter.

erikstokes
  • 12,927
  • 2
  • 36
  • 56
  • 1
    This looks pretty close to what I was imagining, but it seems like it only updates the gutter when the buffer is saved. I wonder how hard it'd be to integrate with (or otherwise act more like) flymake... – Ken Dec 31 '14 at 02:42
  • Ditto. This would be even better if it would automatically add + as soon as you make changes rather than wait for pressing all those buttons to save. – Sridhar Sarnobat Jul 27 '20 at 23:43
10

You should check out Ediff. It can both display diffs and act as a (3-way) merge tool. It lets you easily revert changes, and also highlights refined differences to show you only the parts of each hunk that actually changed.

screenshot of ediff

ediff-revision will let you easily compare against the most recent commit, or any previous commits.

lily
  • 375
  • 1
  • 8