在git-diff的输出中着色空格 [英] Coloring white space in git-diff's output
问题描述
关于代码格式我是一种纯粹的:)。我经常删除不必要的空格(行只有ws,ws在行尾等)。我甚至已经设置vim显示这种线条变为红色。
我的问题是使用git-diff我经常看到这样:
- else {
+ else {
即使我有git-diff颜色,我看不到差异(在特定的情况下,我删除1 ws在行结束)。有什么办法告诉git-diff显示ws变成红色? (例如与/ \s + $ / regexp匹配的那些)。
您可能需要设置color.diff。空格配置设置,例如与:
git config color.diff.whitespacered reverse
pre>
(假设您已经拥有
color.diff
或颜色。 ui 设置为
auto
,因为你说你看到从git diff
)
如果您要微调以红色突出显示的空格错误类型,您可以更改
core.whitespace
,但默认情况下启用blank-at-eol
,因此您可能不需要更改您提到的示例。
一个可能的混乱的原因是在
git diff
的输出中,空格错误只在引入的行中突出显示,被去除。 (更新:,因为Paul Whittaker在其答案中指出 ,您应该将其投票: ),你可以通过使用git diff -R
颠倒差异的意义来看到这些。)
请在 git config手册页中找到有关这些配置选项的更多文档
如果您不想使用
-R
kludge,您可以使用 WhiteSpace错误突出显示 差异手册页。
- ws-error-highlight =
突出显示由color.diff.whitespace指定的颜色指定的行上的空格错误。
是一个逗号分隔的旧的,新的,上下文的列表。当不给出此选项时,
只会突出显示新行中的空格错误。例如。
--ws-error-highlight = new,old突出显示已删除和添加的行上的空格错误。所有可以用作
old,new,context的简写。
git diff --ws-error-highlight = new,old< file>
或
$ b b
git diff --ws-error-highlight = all< file>
我知道一个方法永久打开和存储在config除了使用别名:
git config alias.df'diff - -ws-error-highlight = all'
现在您可以使用:
git df< file>
以红色显示更改。
请注意,使用Git 2.11(2016年第4季度)的,此别名可能会被替换为:
git config diff.wsErrorHighlight all
请参阅 doc on
git diff
和git config
。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).
解决方案You may need to 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
orcolor.ui
set toauto
since you say that you see coloured patches fromgit diff
anyway.)If you want to fine tune the type of whitespace errors that are highlighted in red, you can then change
core.whitespace
, butblank-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 withgit 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=
Highlight whitespace errors on lines specified by in the color specified by color.diff.whitespace. is a comma separated list of old, new, context. When this option is not given, only whitespace errors in new lines are highlighted. E.g. --ws-error-highlight=new,old highlights whitespace errors on both deleted and added lines. all can be used as a short-hand for old,new,context.
git diff --ws-error-highlight=new,old <file>
or
git diff --ws-error-highlight=all <file>
I don't know of a 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.
Note that with Git 2.11 (Q4 2016), this alias might be replaced with:
git config diff.wsErrorHighlight all
See doc on
git diff
and ongit config
.这篇关于在git-diff的输出中着色空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!