Coloring white space in git-diff's output
GitColorsDiffWhitespaceGit Problem Overview
Regarding code formatting I'm kind of purist :). I very often remove unnecessary white spaces (lines with only ws, ws at the end of lines etc). I even have set vim to show that kind of lines colored to red.
My problem is that using git-diff I often see something like this:
- else{
+ else{
Even if I have git-diff colored I can't see difference (in that particular situation I removed 1 ws at the end of line). Is there any way to tell git-diff to show that ws colored to red? (for example those matched with /\s+$/ regexp).
Git Solutions
Solution 1 - Git
With with Git 2.11 (Q4 2016) and after, you can do:
git config diff.wsErrorHighlight all
See doc on git diff
and on git config
.
For versions older than that, you can set the color.diff.whitespace
config setting, e.g. with:
git config color.diff.whitespace "red reverse"
(I'm assuming that you already have color.diff
or color.ui
set to auto
since you say that you see coloured patches from git diff
anyway.)
If you want to fine tune the type of whitespace errors that are highlighted in red, you can then change core.whitespace
, but blank-at-eol
is enabled by default so you probably won't need to change that for the example you mention.
A possible source of confusion is that in the output of git diff
, whitespace errors are only highlighted in the lines that are introduced, not those that are removed. (Update: as Paul Whittaker points out in his answer, which you should up-vote :), you can see these by reversing the sense of the diff with git diff -R
.)
You can find more documentation on these config options in the git config man page
If you don't want to use the -R
kludge you can use the WhiteSpace Error Highlight option from the diff man page.
> --ws-error-highlight=
git diff --ws-error-highlight=new,old <file>
or
git diff --ws-error-highlight=all <file>
With versions older than 2.11, there’s no way to permanently turn this on and store this in config aside from using an alias:
git config alias.df 'diff --ws-error-highlight=all'
Now you can use:
git df <file>
To see the changes in red.
Solution 2 - Git
Use git diff -R
to turn removed lines into added lines. Then trailing whitespace will be highlighted.
(This assumes you already have whitespace hightlighting enabled, as per the colour settings from Mark's answer. Credit for this method goes to Junio's post at http://git.661346.n2.nabble.com/Highlighting-whitespace-on-removal-with-git-diff-td5653205.html .)
For example, when converting a file from DOS line endings to Unix, git diff -R
clearly shows me the ^M
characters (dis)appearing at the ends of lines. Without -R
(and also without -w
etc.) it shows that the entire file has changed, but doesn't show how.
Solution 3 - Git
Use git diff --color | less -R
. The -R
makes the color control codes human-friendly.
Then you can use less
's regular expression search, e.g.
/[[:space:]]+$
Solution 4 - Git
For the lazy answer skimmers, just run:
git config --global diff.wsErrorHighlight all
Then git diff
will highlight trailing whitespaces in removed lines as well.
Solution 5 - Git
My version of git diff
already seems to do this - I have git 1.7.4.1 and have set color.ui = auto
.