目前,当git diff
启用--color
选项时,git会显示行结尾,例如^M
,并且仅在添加的行中显示空格。是否有可能让git在删除的行中显示这些?
答案 0 :(得分:1)
这不是最漂亮的解决方法,但您可以创建一个shell别名以使其可以忍受。遗憾的是,如果在输出上混合制表符和空格,它将无法在终端中正确呈现。这似乎是Gnome终端的一个问题(你会注意到在diff中添加的行也有相同的行为)。
这是你想要的命令:
git diff --color | \
sed 's/^\(\x1B\[31m-.*[^ \t]\)\([ \t]\+\)\(\x1B\[m\)$/\1\x1B[41m\2\3/g'
或者更方便的是,将其添加到您的.bashrc
:
alias coloreol="sed 's/^\(\x1B\[31m-.*[^ \t]\)\([ \t]\+\)\(\x1B\[m\)$/\1\x1B[41m\2\3/g'"
这样你就可以在shell提示符下输入git diff --color|coloreol
。
基本上,它匹配ANSI红色代码,减号后跟任何非空白字符。然后它匹配一个或多个空白字符,直到遇到ANSI重置标志。 Sed将在空格和行尾之间插入ANSI红色反转代码。
我已经为你打破了reg ex,以便你可以根据自己的需要进行修改:
第一部分,分为三组:
Match the start of the line and ANSI code for red:
^\(\x1B\[31m
-.*[^ \t] <-- Followed by the hyphen, anything, then a non-whitespace character
\)
Now match one or more space or tab characters for group 2.
\([ \t]\+\)
Finally the ANSI reset code and EOL for group 3
\(\x1B\[m\)$
然后我们将其替换为:
\1 \x1B[41m \2 \3
即第一个匹配,反转红色ANSI代码(\x1B[41m
),后跟空格和ANSI重置。
答案 1 :(得分:1)
这不是你想要的,但如果你使用Vim,你可以将它添加到你的vimrc文件中,它会显示空白。然后,如果您使用vimdiff或使用Git diff与Vim(我推荐名为Fugitive的插件),它将显示空白。
以下是您必须添加到vimrc的内容:
highlight RedundantWhitespace ctermbg=red guibg=red
highlight ExtraWhitespace ctermbg=red guibg=red
match RedundantWhitespace /[^\t]\zs\t\+/
match ExtraWhitespace /\s\+$\| \+\ze\t/
以下是您可以用来自动删除空白的内容:
autocmd BufWritePre * :%s/\s\+$//e