user256497
user256497

Reputation:

Highlight changed lines and changed bytes in each changed line

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)?

Upvotes: 122

Views: 31454

Answers (15)

Johannes Braunias
Johannes Braunias

Reputation: 3313

As amized wrote in the comment of his answer, without installing extra software, you could use:

git difftool -t vimdiff -y a/a b/a

and then optionally in vim:

:syntax off

Upvotes: 0

madlymad
madlymad

Reputation: 6530

Some of the tools may already mentioned but I will try to summarise and add some extra tips here:

Bash/Terminal

You can use the colordiff or diff-so-fancy

Install

Pick depending on your system

sudo apt-get install colordiff
# or
sudo yum install colordiff

Usage

colordiff firstFile secondFile
# or for word coloring
colordiff -y firstFile secondFile

Git

In case your diff not already highlight the output this can configured with:

git config --global color.ui auto
git config --global color.diff.wordDiff true

Vim

Vim has a build-in highlighting that can be enabled via editing the ~/.vimrc and adding the following lines:

syntax enable
filetype plugin indent on

For highlighting the exact bytes it is required an extra plugin there are multiple plugins available for this purpose ie diffchar.vim or vim-gitgutter

Upvotes: 0

Andy Stewart
Andy Stewart

Reputation: 5498

My vim plugin vim-gitgutter does this when you preview a diff. You can see a simple example in the screenshot in the readme.

The code that calculates the intra-line differences is here.

Upvotes: 0

Tom Hale
Tom Hale

Reputation: 46775

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.

Upvotes: 33

user767082
user767082

Reputation:

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

Upvotes: 80

MForster
MForster

Reputation: 9376

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

Upvotes: 5

Cory Klein
Cory Klein

Reputation: 55670

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.

Upvotes: 19

Zorglub29
Zorglub29

Reputation: 8841

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

Upvotes: 1

Eugen Konkov
Eugen Konkov

Reputation: 25133

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

Upvotes: 7

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.

Upvotes: 1

dshepherd
dshepherd

Reputation: 5407

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.

Upvotes: 17

amized
amized

Reputation: 337

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

$ git diff --color-words | less -RS

Upvotes: 15

Finbar Crago
Finbar Crago

Reputation: 444

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

Upvotes: 2

anydot
anydot

Reputation: 1539

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

Upvotes: 61

PDug
PDug

Reputation: 1252

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.

Upvotes: -1

Related Questions