git
tabwidth
git show
git diff
configuration

setting tabwidth to 4 in git show / git diff

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

If the goal is to make tabs appear as four spaces when viewing git diff or git show, the practical lever is usually the pager, not a general Git diff option. Git emits tabs in the patch text, and the pager or terminal decides how those tabs are visually expanded.

The Practical Fix: Configure the Pager

A common one-off command is:

bash
git -c core.pager='less -x4' diff
git -c core.pager='less -x4' show HEAD

Here, less -x4 tells the pager to render tab stops every four columns.

If you want this behavior all the time, set it globally:

bash
git config --global core.pager 'less -x4'

Then git diff and git show will both render tabs using four-column tab stops when the output goes through less.

Why This Works

Git’s patch output is text that may still contain literal tab characters. The display width you perceive is mostly a function of the viewer, not only of Git itself.

That is why changing the pager often solves the problem immediately. You are not rewriting the patch. You are telling the display tool how to render tab characters.

This also explains why the same diff can look different in:

  • a terminal pager
  • an IDE-integrated Git view
  • a web UI
  • redirected plain-text output

Each viewer can interpret tabs differently.

Make an Alias if You Only Want It for Diffs

If you do not want to change the global pager for every Git command, use an alias:

bash
git config --global alias.diff4 '!git -c core.pager="less -x4" diff'
git config --global alias.show4 '!git -c core.pager="less -x4" show'

Then run:

bash
git diff4
git show4 HEAD

This keeps the adjustment limited to the commands where tab rendering matters most to you.

Do Not Confuse Display Width with Whitespace Rules

Git also has settings involving whitespace and tab width for whitespace checking, such as tabwidth in whitespace-related configuration. That is not the same thing as telling git diff how to visually expand tabs in the pager.

So there are two separate ideas:

  • display width in the viewer
  • whitespace interpretation for checks and highlighting

If your complaint is “the diff looks misaligned on screen,” pager configuration is the first thing to fix.

Editor and Terminal Context Still Matter

If you redirect output to a file or open the diff in another tool, less -x4 no longer controls what you see. The receiving tool may render tabs differently.

That means consistency across all environments may require adjustments in:

  • terminal pager settings
  • IDE Git viewer settings
  • external diff tools
  • terminal emulator tab behavior

There is no single universal switch that forces every viewer everywhere to use four columns.

Common Pitfalls

The biggest mistake is looking for a git diff --tabwidth=4 style display option. For normal diff viewing, the pager is usually the right layer to configure.

Another issue is setting a global pager value and forgetting that it affects other paged Git commands too. If that is undesirable, use aliases or command-local configuration.

Developers also sometimes confuse whitespace-checking settings with visual rendering settings. They solve different problems.

Finally, if the diff is being viewed in a GUI tool or editor, changing less has no effect there. Configure the actual viewer in use.

Summary

  • For terminal viewing, use less -x4 through Git’s pager configuration.
  • A one-off command is git -c core.pager='less -x4' diff.
  • A persistent setting is git config --global core.pager 'less -x4'.
  • Pager-based tab width controls display, not Git’s internal whitespace rules.
  • If another tool displays the diff, configure that tool too because pager settings will not apply.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.