highlight changed lines and changed bytes in each

2019-01-21 03:04发布

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

11条回答
虎瘦雄心在
2楼-- · 2019-01-21 03:27

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

查看更多
Juvenile、少年°
3楼-- · 2019-01-21 03:27

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.

查看更多
爱情/是我丢掉的垃圾
4楼-- · 2019-01-21 03: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.

查看更多
做自己的国王
5楼-- · 2019-01-21 03:29

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)+'
查看更多
做自己的国王
6楼-- · 2019-01-21 03:32

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.

查看更多
贼婆χ
7楼-- · 2019-01-21 03:34

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'
查看更多
登录 后发表回答