35

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
  • 351
  • 3
  • 3

3 Answers3

31

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
  • 48,657
  • 3
  • 76
  • 115
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,686
  • 2
  • 34
  • 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 Chung
  • 375
  • 1
  • 8