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.
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:
Here, less -x4 tells the pager to render tab stops every four columns.
If you want this behavior all the time, set it globally:
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:
Then run:
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 -x4through 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
- Setting up and using Meld as your Git difftool and mergetool
- Should a repository interface expose a clear() method to clear the cache of the implementation?
- Should composer.lock be committed to version control?
- Should Gemfile.lock be included in .gitignore?
- Should I add the Visual Studio 2015 .vs folder to source control?
- Should I be adding the Django migration files in the .gitignore file?
- Should I check in folder node_modules to Git when creating a Node.js app on Heroku?
- Should I git ignore xcodeproject/project.pbxproj file?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack 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.