Open Source project Trac has an excellent diff highlighter — it highlight changed lines and changed bytes in each changed line! See https://trac.transmissionbt.com/changeset/12148 or http://trac.gajim.org/changeset/297ad7711d20bfee1491768640d9bc5384464363 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)?

  • 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 '11 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 '11 at 6:36
  • Looks like a dup of stackoverflow.com/questions/3231759/… – Adam Monsen Oct 10 '12 at 11:03
  • Note: GitHub now offers such a diff tool in its Web GUI: stackoverflow.com/a/25723584/6309 – VonC Sep 8 '14 at 12:01

10 Answers 10

I shared a protip that might help, here it is https://coderwall.com/p/ydluzg

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:

enter image description here

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'

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

  • 1
    --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 :( – Nikolay Frantsev Mar 21 '11 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 '11 at 20:05
  • 4
    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. – Nikolay Frantsev Mar 22 '11 at 8:29
  • You can also use this with git add --patch: stackoverflow.com/questions/10873882/… – naught101 Sep 17 '13 at 1:27
  • The advantage of diff-highlight is that it works well for both word diffs and line diffs. – Ciro Santilli 新疆改造中心 六四事件 法轮功 Jul 5 '14 at 9:27

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

$ git diff --color-words | less -RS
  • 1
    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 '13 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 – xpixelz Jun 10 '13 at 10:32

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.

  • 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 '14 at 18:31
  • 3
    This should work now in git 2.9.0: git config interactive.diffFilter diff-highlight – Thomas Jun 16 '16 at 7:15
  • this command show nothing for me – Vitaly Zdanevich Dec 26 '16 at 10:14

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.

  • This is very interesting, could you explain a bit about these gitconfig options? – caesarsol Mar 9 '17 at 14:24
  • Updated, also adding function dsf(). – Tom Hale Mar 10 '17 at 6:03

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)+'

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
  • sorry, I do not want to use emacs, only bash, git or vim – Nikolay Frantsev Mar 23 '11 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... – Finbar Crago Mar 23 '11 at 22:27
  • I gave your problem a bit more thought and have appended a second solution which requires only dwdiff. – Finbar Crago Mar 23 '11 at 23:51
  • 1
    please read carefully my question, I do not want to compare files – Nikolay Frantsev Mar 24 '11 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 – Finbar Crago Mar 24 '11 at 21:56

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.

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.

  • unfortunately, it does not highlight changed bytes on diff output (set filetype=diff) – Nikolay Frantsev Mar 16 '11 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 '11 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 '11 at 15:04
  • unfortunately no, I want to use recursive diff output for multiple files – Nikolay Frantsev Mar 16 '11 at 15:09

vimdiff file1 file2 will display the difference character-wise between two files.

vimdiff is a diff tool included into vim. (Vim should have been compiled with the +diff option, to be sure you can check with :version )

You can also launch it from inside vim. See :help diff for more information and commands.

  • I do not want to compare files, I want to highlight diff (patch) file. – Nikolay Frantsev Mar 16 '11 at 14:07
  • @Nikolay Frantsev If you don't care about perfomance, you can install my format.vim plugin and do vimdiff file.old file.new -c 'FormatCommand diffformat' -c 'w! file.diff.html' -c 'qa!'. – ZyX Mar 16 '11 at 17:42
  • It will do a diff in a batch mode (prepend screen -D -m or append &>/dev/null (/dev/null variant sometimes produces strange bugs) if you don't want to see the terminal flashing) and quit vim after formatting is done, but it is pure vimscript and even with my optimizations it is very slow for large files. – ZyX Mar 16 '11 at 18:02

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.