summaryrefslogtreecommitdiffstats
path: root/highlight-vc-diffs.post
blob: 72bfe33898b4ad80502b8e9832ecc27aa568c262 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
;;;;;
title: highlight VC diffs
tags: tips, emacs, git, vc, diff
date: 2013-01-25 02:20
format: md
;;;;;

Sometimes you come across these gems of packages that seem to fulfill
a wish that you didn't even realise you had.

Today I came across
[git-gutter](https://github.com/syohex/emacs-git-gutter) for Emacs and
its companion
[git-gutter-fringe](https://github.com/syohex/emacs-git-gutter-fringe),
which are apparently based on an extension for Sumblime Text 2. These
show the status of changes in a special "gutter" next to the fringe or
in the fringe itself. This is very cool stuff.

To enable it I added the following code to my Emacs init file:

``` emacs-lisp
(eval-after-load "git-gutter" '(load "git-gutter-fringe"))

(defun maybe-use-git-gutter ()
  "Run `git-gutter' if the current file is being tracked by git."
  (when (eq (vc-backend (buffer-file-name)) 'Git)
    (git-gutter)))

(add-hook 'after-save-hook 'maybe-use-git-gutter)
(add-hook 'after-change-major-mode-hook 'maybe-use-git-gutter)
(add-hook 'window-configuration-change-hook 'maybe-use-git-gutter)
```

`git-gutter` was easily installed through MELPA, but I had to download
`git-gutter-fringe` separately and use `package-install-file` to
install that.

I had to load `git-gutter-fringe` manually because it didn't seem to
have any autoloads defined, and I had to run it using these three
hooks because the information seemed to disappear if I switched
windows and came back, didn't automatically update after saving and
didn't automatically show anything when first loading the file. This
was all fine, since just calling the function updates the buffer it's
easy to use this way.

Later, though, I stumbled upon
[diff-hl](https://github.com/dgutov/diff-hl) by accident. I was
looking for anything involving the fringe, which I think, for the most
part, is underused.

It does pretty much the same thing, except that it does so at least
for git, bazaar and mercurial (according to the readme). It also
defines some commands for working with the blocks of changes, like
navigating between them and reverting them. It uses the fringe by
default, and doesn't require a separate package to be installed. And
it's much easier to set up.

It can also be installed through MELPA, and afterwards it's just a
line in our init file away:

``` emacs-lisp
(global-diff-hl-mode)
```

Of course if you don't want it enabled globally you can call
`diff-hl-mode` from some hooks, but this way works fine for me.

It's funny how I had no idea these existed, didn't even think about
needing/wanting this feature and then finding two of them in the same
day.

<!-- Local Variables: -->
<!-- mode: markdown -->
<!-- End: -->