Carlos Campderrós
Carlos Campderrós

Reputation: 23002

setting tabwidth to 4 in git show / git diff

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=4 and all the indentation seems correct.

But in git diff or git show that's what appears:

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 diff or git show where 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 zsh instead of bash.

Any ideas?

Upvotes: 99

Views: 17882

Answers (3)

Sardorbek Imomaliev
Sardorbek Imomaliev

Reputation: 15400

On MacOS you could do

$ git config --global core.pager 'less --tabs 4'

-x/--tabs is the same option I prefer to use full ones. From man less

-xn,... or --tabs=n,...
Sets tab stops. If only one n is specified, tab stops are set at multiples of n. If multiple values separated by commas are speci- fied, tab stops are set at those positions, and then continue with the same spacing as the last two. For example, -x9,17 will set tabs at positions 9, 17, 25, 33, etc. The default for n is 8.

Upvotes: 10

codemonkee
codemonkee

Reputation: 3011

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

References:

Upvotes: 143

adius
adius

Reputation: 14992

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

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)

Upvotes: 5

Related Questions