git diff and tabs

Have you ever been annoyed when looking at the output of git diff when you notice that columns don’t line up properly when comparing text files that mix the use of tabs and spaces?

What you are seeing is that unified diffs start each line with a space, plus, or minus. A tab character will take you to the next tabstop which is now seven spaces from the beginning of the line. Whereas, if the code has eight spaces, you will be moved eight spaces forward.

The solution is to tell your pager (more or less) to change where to put the tabstops. More and less have a compatible argument -x for doing just that:

:; export GIT_PAGER='more -x9,17,25'

This will fix all of the spacing.

It does raise another issue, though, which should be considered when commiting to a coding style. It is always a good idea to set the max line length to two less than the minimum terminal size you expect coders to use. That provides space for the single character at the beginning of unified diffs and the two characters when looking at three way merge diffs during merge or rebase conflicts.

Written on August 6, 2021