bash 在 git show / git diff 中将 tabwidth 设置为 4

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10581093/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 02:16:18  来源:igfitidea点击:

setting tabwidth to 4 in git show / git diff

gitbashtabswhitespacespaces

提问by Carlos Campderrós

At work we are several developers and don't have a code style guide, and some developers indent with tabs, and some others with 4 spaces (luckily noone of the indent with spaces people uses different than 4 spaces). In general this is no (big) problem because in our editors we set tabwidth=4and all the indentation seems correct.

在工作中,我们有几个开发人员并且没有代码风格指南,一些开发人员使用制表符缩进,而其他一些开发人员使用 4 个空格(幸运的是,没有人使用的缩进与 4 个空格不同)。一般来说,这不是(大)问题,因为在我们的编辑器中,我们设置了tabwidth=4所有缩进似乎都是正确的。

But in git diffor git showthat's what appears:

但在git diffgit show这就是出现的内容:

diff --git a/mesclatabs.php b/mesclatabs.php
new file mode 100644
index 0000000..1986c91
--- /dev/null
+++ b/mesclatabs.php
@@ -0,0 +1,5 @@
+<?php
+function foo() {
+       echo "line with 1 tab\n";
+    echo "line with 4 spaces\n";
+}

The problem is git diffor git showwhere each tabs appears as wide as 8 spaces (well, in reality appears as a tab, and the shell (bash in my case) is showing the tab as 8 spaces. I suppose there must be some bash config to change this, but I'd like to know if git has an option to output tabs as 4 spaces in diff/ show, as some developers work with zshinstead of bash.

问题是git diff或者git show每个选项卡的宽度为 8 个空格(好吧,实际上显示为一个选项卡,并且外壳(在我的情况下为 bash)将选项卡显示为 8 个空格。我想必须有一些 bash 配置可以更改这个,但我想知道 git 是否可以选择将制表符输出为diff/ 中的4 个空格show,因为一些开发人员使用zsh而不是bash.

Any ideas?

有任何想法吗?

回答by codemonkee

I believe git config --global core.pager 'less -x1,5'

我相信 git config --global core.pager 'less -x1,5'

References:

参考:

回答by adius

As the answer https://stackoverflow.com/a/10584237/1850340did not work for me because of my color settings I came up with following solution:

作为答案https://stackoverflow.com/a/10584237/1850340由于我的颜色设置对我不起作用,我想出了以下解决方案:

TAB=$'\t' && git config --global core.pager "sed 's/$TAB/    /g' | less" && unset TAB

This replaces all tab characters with 4 spaces before displaying it with less. (The TAB workaround is needed to circumvent the shells backslash escape)

这将用 4 个空格替换所有制表符,然后用更少的空格显示。(需要 TAB 解决方法来规避 shell 反斜杠转义)