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)?
While using
git diff
orgit log
and possibly others, use option--word-diff=color
(there are also other modes for word diffs BTW)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.diff-so-fancy
is adiff
-highlighter designed for human eyeballs.It removes the leading
+
/-
which are annoying for cut/paste and makes clear sections between files.Coloured
git
(left) vsdiff-so-fancy
(right - note the character-level highlights):If you want the
diff-so-fancy
(right side) output but not constrained to files in agit
repository, add the following function to your.bashrc
to use it on any files:Eg:
Character level highlighting and standard
diff
formatIf you don't like the non-standard formatting of
diff-so-fancy
, but still want character-levelgit
highlighting, usediff-highlight
which will takegit
's output and produce the really pretty standarddiff
-format output:To use it by default from
git
, add to your.gitconfig
:The
[pager]
section tellsgit
to pipe its already colourised output todiff-highlight
which colourises at the character level, and then pages the output in less (if required), rather than just using the defaultless
.as @dshepherd says:
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):Then configure your
.gitconfig
as official doc says:UPD
Also you can try next on latest
git
without any installation:More complex:
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.
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:Install with:
Move the file
diff-highlight
to the~/bin/
directory (or wherever your$PATH
is), and then add the following to your~/.gitconfig
:Single copy paste install suggested by @cirosantilli: