100

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?

5
  • 8
    in an ideal world the devs would all use spaces and you won't have silly issues like this. Commented May 14, 2012 at 10:38
  • 5
    I completely disagree. Why would you represent something with 4 characters when you can represent it with 8? Commented May 24, 2013 at 14:59
  • Of course I don't know your situation, but I would really push for some super-basic coding guidelines, maybe just about the tabs/spaces. No tabs sounds like a good idea, because then people tend to use tabs to align their comments and such, too, and it becomes a mess... And inserting tabs after spaces... It's confusing when I go to edit and I can't see what's a tab and what's a space (so I always make tabs visible in my editor). I'm a bit neurotic about it, though, so </opinion> Commented Sep 13, 2018 at 18:42
  • 1
    Also, I think it's possible to use (only) tabs consistently for indenting code, and spaces for any other alignment, and then people can change the tabstop to whatever they want. I can't imagine people being consistent enough, though! Commented Sep 13, 2018 at 18:43
  • @MatthewG 8 sounds a lot grander than my 2... Thank you for showing me the light! :D Commented Sep 13, 2018 at 18:44

3 Answers 3

143

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

References:

Sign up to request clarification or add additional context in comments.

14 Comments

The argument to less should be -x5 because the +/- symbols of the unified diff don't impact the location of the tab-stop, but they do push all the spaces one character to the right. A tab-stop of 5 aligns everything properly.
@ZhengKai: Just as a data point, I have less aliased as less -R, core.pager defined as above, and I run git diff --color-words -- everything works fine and the tab width is correct. Maybe that helps?
When I do git add -p the pager is not used and the tab displayed in wrong way. If you describe how to fix this too. then the answer will be full
@IanRogers - For colour escape sequences you'd want to use less -R / --RAW-CONTROL-CHARS linux.die.net/man/1/less
For those wondering, in -x1,5 the 1 is for the leading column in the diff output (that contains +, - and a space). Subsequent tabs then jump to columns 5, 9, 13, 17, etc - aka 1 + multiples of 4. So, for git diff the right argument is definitely -x1,5. People claiming above that -x5 works for them ... well - that's a tab width of 5 spaces without a leading column, hmm. Unlikely?
|
11

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.

Comments

5

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)

1 Comment

I'm not on something I can test with, but would this be where less -R would come into play? to allow the ANSI colour escape sequences through, opposed to less -r?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.