16

I was wondering if it's possible to change the tabsize in git-gui from 8 characters to 4. The only thing I found was core.whitespace = tabsize=4 but that only affects diff.

The reason why I'm asking is because I'm slowly phasing out the usage of tabs and instead use 4-space indentation. This however can give confusing results when watching the diff in git-gui, as tabsize is around 8 characters.

I know a simple :retab in Vim solves my problem, but quite frequently I've got to work on big files with a long-term history which may have modifications in separate branches. So I can't always simply convert all tabs.

3 Answers 3

7

As of git-gui-0.20 and git 2.4.0 (commit) a configuration parameter exists which has been added to configure the displayed tabsize in git gui.

Add this to your ~/.gitconfig

[gui]
    tabsize = 4

or change it via the configuration menu of git-gui (Edit -> Options...)

2
  • 1
    Thanks. I updated my Git (was at 1.8 :-O) and I see this option. I still have to deal with a mixmatch of tabs and spaces every now and then so this is great.
    – Htbaa
    Feb 11, 2016 at 9:45
  • git config --global gui.tabsize 4 does the configuration job on the command line
    – koppor
    Aug 30, 2023 at 18:53
10
+50

I did some research.

The configuration core.whitespace has nothing to do with how tabs are displayed. It is only used for git to recognize tab related whitespace errors. For example: when indent-with-non-tab is enabled, and core.whitespace is set to tabwidth=4, and a line is indented using 4 spaces or more, then git will report an error.

For git, apart from the tab related whitespace error detection features, a tab character is a character like any other character. It gets compared and it gets dumped to whatever tool is used to display the characters. That means, to configure the displayed tab width you have to configure the "front ends" of git. In case of git diff that would be "less" (the unix tool "less"). In case of git gui that would be git-gui itself.

Configuring less is easy. You can set the git configuration core.pager to setup less to display a tab using 4 spaces (less has a parameter -xn to set tabwidth to n).

Configuring git-gui turned out to be considerably harder. Git-gui is written in Tcl/Tk. I found a Tcl/Tk option to configure the tab width in text widgets. I also found a line in git-gui.sh where it looks like the text widget is being initialized.

This is line 3346 of the file git-gui.sh in git version 1.7.5:

catch {$ui_diff configure -tabstyle wordprocessor}

I changed that, according the Tcl/Tk manual, to:

catch {$ui_diff configure -tabs "[expr {4 * [font measure $font 0]}]" -tabstyle wordprocessor}

That did not seem to have any effect. I tried different values for -tabs and they did have some effect on displayed tab width, so it seems to be the correct line to modify.

Unfortunately, Tcl/Tk does not seem to have a notion of tab width in terms of characters, instead, the tab width has to be set in pixels or centimeters.

Note that I have no experience in Tcl/TK whatsoever, so maybe I am just overlooking something simple here.

Anyway, now you know where to start digging. Remember to report back here if you have a working solution. Good luck.

3
  • Thank you for your research. I'll have to reserve some time for this to give it a try. The bounty is yours as deserved. When and if I figure out how to do it I'll be sure to report back here.
    – Htbaa
    May 3, 2011 at 16:45
  • @Htbaa: I think you have to click the "award bounty" button to award the bounty. Accepting the answer does not award the bounty.
    – Lesmana
    May 4, 2011 at 6:52
  • Thanks. Done! I was pretty sure the bounty was given away.
    – Htbaa
    May 4, 2011 at 12:20
5

I elaborated research made by lesmana.

The line

catch {$ui_diff configure -tabstyle wordprocessor}`

mentioned by lesmana should be replaced with following:

catch {$ui_diff configure -tabs "[expr {[get_config gui.tabsize] * [font measure font_diff 0]}] left" -tabstyle wordprocessor}

After doing this one can tune tab size using option "tabsize" in [gui] section of git configuration file (aka gui.tabsize). Bad news:

  1. Some tabs after last non-whitespace character turn into simple spaces. This looks like a bug in tcl.
  2. One has to restart git-gui after changing font size to get correct tabs. Who knows how to resolve it?

Tested with:

  • git-gui version 0.13.0.8.g8f85
  • git version 1.7.4.1
  • tcl version 8.4.16-2 (Ubuntu 11.04 i386 package)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .