115

Open Source project Trac has an excellent diff highlighter — it highlights changed lines and changed bytes in each changed line! See here or here for examples.

Is there way to use the same color highlight (i.e. changed lines and changed bytes too) in bash terminal, git, or vim for diff output (patch-file)?

6
  • What is it that you want to highlight? Do you want a diff tool that higlights the byte changes? (that would be very helpful). You say vim, to my recollection vim does a lot of color manipulation already when you are using programming language templates (and other). How would you change that? There are quite a few techniques available to change color on a terminal window that is defined VT100 (and there are dozens of other definitions that will also support color escape sequences). More specifics please. Or read en.wikipedia.org/wiki/VT100 and related links. Maybe that can help.
    – shellter
    Mar 16, 2011 at 14:00
  • I know you are only interested in open source tools, and only in terminal. But just as a reference point you may want to look at slickedit's diffzilla. of the few diff tools I used it has always seem to best represent character differences (though it definitly had issues when the diffs where complex (combination of formatting and code changes, which is always a bad idea)
    – nhed
    Mar 19, 2011 at 6:36
  • Looks like a dup of stackoverflow.com/questions/3231759/… Oct 10, 2012 at 11:03
  • Note: GitHub now offers such a diff tool in its Web GUI: stackoverflow.com/a/25723584/6309
    – VonC
    Sep 8, 2014 at 12:01
  • I have posted 'yet another' pure git, diff-highlight based solution with tutorials to easily 1) find the relevant diff-highlight file, 2) make it executable 3) set necessary params in .gitconfig. Please have a look. Instructions are for Ubuntu 18.04 but should work broadly on linux systems.
    – Zorglub29
    Apr 1, 2020 at 12:30

12 Answers 12

79

The diff-highlight Perl contrib script produces output so similar to that of the Trac screenshots that it is likely that Trac is using it:

screenshot of diff-highlight in use

Install with:

wget https://raw.githubusercontent.com/git/git/fd99e2bda0ca6a361ef03c04d6d7fdc7a9c40b78/contrib/diff-highlight/diff-highlight && chmod +x diff-highlight

Move the file diff-highlight to the ~/bin/ directory (or wherever your $PATH is), and then add the following to your ~/.gitconfig:

[pager]
    diff = diff-highlight | less
    log = diff-highlight | less
    show = diff-highlight | less

Single copy paste install suggested by @cirosantilli:

cd ~/bin
curl -O https://raw.githubusercontent.com/git/git/fd99e2bda0ca6a361ef03c04d6d7fdc7a9c40b78/contrib/diff-highlight/diff-highlight
chmod +x diff-highlight
git config --global pager.log 'diff-highlight | less'
git config --global pager.show 'diff-highlight | less'
git config --global pager.diff 'diff-highlight | less'
git config --global interactive.diffFilter diff-highlight
10
  • 28
    Ah, this is part of core git now: github.com/git/git/tree/master/contrib/diff-highlight
    – naught101
    Mar 26, 2013 at 5:57
  • 3
    It's now been turned into a module, and I think the easiest version to download from is the one immediately before that change at raw.githubusercontent.com/git/git/… Jun 29, 2017 at 8:09
  • 5
    Not only is this part of core git, it's distributed with git and probably already on your system. I added details on how to enable it in my answer below. ↓
    – Cory Klein
    Jan 17, 2019 at 17:02
  • 5
    This misses the diffs you see via git add -p. Please also add: git config --global interactive.diffFilter diff-highlight
    – josch
    Mar 25, 2019 at 12:53
  • 2
    One may need to run make in /usr/share/doc/git/contrib/diff-highlight to get /usr/share/doc/git/contrib/diff-highlight/diff-highlight (see this commit)
    – ricab
    Jan 26, 2022 at 13:11
53

While using git diff or git log and possibly others, use option --word-diff=color (there are also other modes for word diffs BTW)

6
  • 3
    --word-diff=color is really better (especially with git config color.diff.old "red reverse" and git config color.diff.new "green reverse"), but it is not what I want :(
    – user256497
    Mar 21, 2011 at 11:31
  • 4
    So only thing you are missing is marking in colour/somehow both changed lines and bytes on same time?
    – anydot
    Mar 21, 2011 at 20:05
  • 7
    I want to highlight changed lines and changed bytes in each changed line, like in Trac. Not just changed bytes, it is not the same.
    – user256497
    Mar 22, 2011 at 8:29
  • You can also use this with git add --patch: stackoverflow.com/questions/10873882/…
    – naught101
    Sep 17, 2013 at 1:27
  • The advantage of diff-highlight is that it works well for both word diffs and line diffs. Jul 5, 2014 at 9:27
29

diff-so-fancy is a diff-highlighter designed for human eyeballs.

It removes the leading +/- which are annoying for cut/paste and makes clear sections between files.

Coloured git (left) vs diff-so-fancy (right - note the character-level highlights):

diff-so-fancy output

If you want thediff-so-fancy (right side) output but not constrained to files in a git repository, add the following function to your .bashrc to use it on any files:

dsf() { git diff --no-index --color "$@" | diff-so-fancy; }

Eg:

dsf original changed-file

Character level highlighting and standard diff format

If you don't like the non-standard formatting of diff-so-fancy, but still want character-level git highlighting, use diff-highlight which will take git's output and produce the really pretty standard diff-format output:

diff-highlight screenshot

To use it by default from git, add to your .gitconfig:

[color "diff-highlight"]
  oldNormal = red bold
  oldHighlight = red bold 52
  newNormal = green bold
  newHighlight = green bold 22

[pager]
  diff = diff-highlight | less -FRXsu --tabs=4

The [pager] section tells git to pipe its already colourised output to diff-highlight which colourises at the character level, and then pages the output in less (if required), rather than just using the default less.

3
17

A utility for byte-based diffs has been distributed with official Git since v1.7.81. You just have to locate where it is installed on your machine and enable it.

Find where Git is installed

  • MacOS with Git installed via Homebrew: It's /usr/local/opt/git (later versions: /opt/homebrew/Cellar/git/VERSION)
  • Windows with Git for Windows: Run cd / && pwd -W to find the install directory.
  • Linux: Nerd. If you don't already know where Git is installed, then ll $(which git) or locate git should help.

Link diff-highlight to your bin directory so that your PATH can find it

GIT_HOME='/usr/local/opt/git/'  # Use the value from the first step.
ln -s "${GIT_HOME}/share/git-core/contrib/diff-highlight/diff-highlight" \
      '/usr/local/bin/diff-highlight'

Enable it in your Git config

git config --global interactive.diffFilter diff-highlight # Use on interactive prompts
git config --global pager.diff "diff-highlight | less"    # Use on git diff
git config --global pager.log  "diff-highlight | less"    # Use on git log
git config --global pager.show "diff-highlight | less"    # Use on git show

1 Here is the v1.7.8 version, but lots of changes have been made since then.

4
  • 1
    It would be good to specify in which version it started to be distributed with git. Also l'm guessing that distros will put it in PATH by default, so the symlink step won't be needed? And which git requires it to be in the PATH in the first place so it won't work if it's not :-) Mar 12, 2019 at 13:02
  • 2
    It would be good! Feel free to add that information. And although Git does bundle diff-highlight, it does not actually install it, so the symlink step is indeed necessary (at least on macOS). If you find that it isn't necessary for your platform, again feel free to update the answer. Meanwhile, which git usually does work, because Git does install the git binary somewhere on the path.
    – Cory Klein
    Mar 12, 2019 at 14:42
  • 2
    Note that in debian unstable I needed to "compile" this file, because I just had a .perl. The compilation is trivial: just run sudo make in the diff-highlight directory.
    – tobiasBora
    Apr 1, 2020 at 16:57
  • This seems to imply Git for Windows will install the contrib content, but it does not. diff-highlight is not installed by the Git for Windows installer nor is there any option to do so. Oct 9, 2022 at 21:01
16

The behaviour you want is now available in git itself (as was pointed out in a comment by naught101). To enable it you need to set your pager to

perl /usr/share/doc/git/contrib/diff-highlight/diff-highlight | less

where /usr/share/doc/git/contrib/diff-highlight/diff-highlight is the location of the highlighter script on Ubuntu 13.10 (I have no idea why it's in a doc folder). If it isn't there on your system try using locate diff-highlight to find it. Note that the highlighting script is not executable (at least on my machine), hence the requirement for perl.

To always use the highlighter for the various diff-like commands just add the following to your ~/.gitconfig file:

[pager]
    log = perl /usr/share/doc/git/contrib/diff-highlight/diff-highlight | less
    show = perl /usr/share/doc/git/contrib/diff-highlight/diff-highlight | less
    diff = perl /usr/share/doc/git/contrib/diff-highlight/diff-highlight | less

I added this as a new answer naught101's comment is buried and because the set up is not quite as trivial as it should be and at least on the version of Ubuntu that I have the instructions in the README don't work.

3
  • I've just noticed that this doesn't enable the highlighting for the diffs within git add -p (interactive mode). I don't know how that can be fixed though, simply adding add to the list causes it to hang.
    – dshepherd
    Sep 17, 2014 at 18:31
  • 5
    This should work now in git 2.9.0: git config interactive.diffFilter diff-highlight
    – Thomas
    Jun 16, 2016 at 7:15
  • ^ This! Unfortunately, diff-highlight wasn't on my path so I had to locate it first. Details in my answer below.
    – Cory Klein
    Jan 17, 2019 at 17:01
12

I use --color-words option and it works fine for me :

$ git diff --color-words | less -RS
2
  • 6
    No, this only shows the difference in words. What the OP (and I) want is a normal line-by-line diff, with the word differences highlighted (so, say different lines are coloured text, and the word-differences within those lines are normal coloured text, with coloured highlighting or something). See the example links now in the question.
    – naught101
    Mar 26, 2013 at 5:21
  • 1
    pastebin.com/1JrhYHRt Actually I use vimdiff as difftool and vimdiff with molokai colorscheme to get a nice highlighting as you describe in your question. 1- git config --global diff.tool vimdiff 2- in vim ":colo molokai" * Molokai @ github.com/tomasr/molokai * Possible auto colorscheme with ~/.vimrc: if &diff set background=dark colorscheme molokai endif
    – amized
    Jun 10, 2013 at 10:32
7

as @dshepherd says:

The behaviour you want is now available in git itself

But diff-highlight is located in DOC and is not available from shell.
To install diff-highlight into your ~/bin directory follow next steps (This will save your typing):

$ locate diff-highlight
$ cd /usr/share/doc/git/contrib/diff-highlight  #or path you locate
$ sudo make
$ mv diff-highlight ~/bin

Then configure your .gitconfig as official doc says:

[pager]
    log  = diff-highlight | less
    show = diff-highlight | less
    diff = diff-highlight | less

UPD
Also you can try next on latest git without any installation:

git diff --color-words=.

More complex:

git diff --color-words='[^[:space:]]|([[:alnum:]]|UTF_8_GUARD)+'
4

In an answer to a similar, but slightly different question, I suggest using Delta, which is a modern diff postprocessing tool that specifically supports the special desire for highlighting both words and lines at the same time.

Delta is highly configurable (with emulation modes for diff-highlight and diff-so-fancy) and includes many features not found in other tools: side-by-side views, syntax highlighting, and coloring of merge conflicts and git blame output.

The Delta documentation also has an overview of related projects that mentions a few more ad-hoc tools that can highlight both words and lines.

Delta diff formatting example

2

Emacs has the ediff-patch-buffer function which should fulfill your needs.

Open the un-patched file in emacs type ESC-x, ediff-patch-buffer.

Follow the prompts and you should see a highlighted comparison of the patched and original versions of your file.

As per your comment the following will will give you a bash solution requiring only dwdiff:

#!/bin/bash
paste -d'\n' <(dwdiff -2 -L -c <(cat $2) <(patch $2 -i $1 -o -)) <(dwdiff -1 -L -c <(cat $2) <(patch $2 -i $1 -o -))| uniq
6
  • sorry, I do not want to use emacs, only bash, git or vim
    – user256497
    Mar 23, 2011 at 8:31
  • That's understandable. The only other thing i can think of is to use colordiff with the stdout from patch: colordiff -u <(patch original_file -i patch_file -o -) <(cat original_file) but this is only going to highlight changed lines not bites... Mar 23, 2011 at 22:27
  • I gave your problem a bit more thought and have appended a second solution which requires only dwdiff. Mar 23, 2011 at 23:51
  • 1
    please read carefully my question, I do not want to compare files
    – user256497
    Mar 24, 2011 at 8:47
  • 1
    sorry for the confusion, so your just after a way to highlight the changed bytes on the changed lines of a diff file? if so try dwdiff -c --diff-input diff_file Mar 24, 2011 at 21:56
1

Diffy

GitLab is using Diffy https://github.com/samg/diffy (Ruby) to achieve output similar to GitHub and diff-highlight:

enter image description here

Diffy makes the diff itself using the same algorithm ad Git, and supports different types of outputs, including the HTML output that GitLab uses:

gem install diffy
echo '
  require "diffy"    
  puts Diffy::Diff.new("a b c\n", "a B c\n").to_s(:html)
' | ruby

Output:

<div class="diff">
  <ul>
    <li class="del"><del>a <strong>b</strong> c</del></li>
    <li class="ins"><ins>a <strong>B</strong> c</ins></li>
  </ul>
</div>

Note how strong was added to the changed bytes.

1

Note: this is a duplicate of what is found here: How to improve git's diff highlighting? . Posting my answer here too though, as it may be helpful to some people who find directly this thread :)

As said in some previous answers, this is possible with only git stuff. I post this as the instructions may be a bit easier to follow depending on your system, but this is similar to several other answers.

One solution that is purely relying on git and its contribs. This requires no additional files than what comes with git. All explanations are for Ubuntu (tested on 18.04LTS), should work similarly on other linux systems:

  • Locate the diff-highlight contrib git snippet:
find -L /usr -name diff-highlight -type f

on my system the only valid answer is:

/usr/share/doc/git/contrib/diff-highlight/diff-highlight
  • Make the corresponding perl script executable. In my case I needed to do:
sudo chmod +x /usr/share/doc/git/contrib/diff-highlight/diff-highlight
  • Update your ~/.gitconfig to get the result you want, by adding (note these are TABS, not 4 spaces):
[color "diff-highlight"]
    oldNormal = red
    oldHighlight = red 52
    newNormal = green
    newHighlight = green 22
  • Enjoy the result (note: this is only for the diff coloring + highlight, I have other things at play here too for the prompt of course :) ).

diff-highligh

-1

Yes, Vim does this including the highlighting of text changed within a line.
See :h diff and :h 08.7 for more details on how to diff files.

Vim uses a fairly simple algorithm for it's highlighting. It searches the line for the first changed character, and then the last changed character, and simply highlights all characters between them.
This means you can't have multiple highlights per line - many design decisions in Vim prioritise efficiency.

4
  • unfortunately, it does not highlight changed bytes on diff output (set filetype=diff)
    – user256497
    Mar 16, 2011 at 14:08
  • 1
    I think I understand your question now - You want to syntax highlight the textual output of the diff command so that it highlights any changes made inside of a line. Editing this text in Vim highlights line differences, but not the changes made within a line.
    – PDug
    Mar 16, 2011 at 14:55
  • Could you use Vim's :patchfile command to load the original file and then compare it to the patched version?
    – PDug
    Mar 16, 2011 at 15:04
  • unfortunately no, I want to use recursive diff output for multiple files
    – user256497
    Mar 16, 2011 at 15:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy