346

When I view files on GitHub, tabs appear as 8 spaces.

Example:

example

Is that possible to change this configuration to 2 or 4 spaces?

Abraham
  • 8,525
  • 5
  • 47
  • 53
Misha Moroshko
  • 166,356
  • 226
  • 505
  • 746
  • 8
    You can also check out answers provided in the GitHub issue [#170 `pre { tab-size: 4 }`](https://github.com/isaacs/github/issues/170) – KyleMit Nov 03 '14 at 22:19
  • 2
    I think you should change the accepted answer to the one by @rofrol about using the `.editorconfig`, I think his answer includes current best methods for both setting the configuration in a way that other people will see the code as you intended it to be seen, and altering how other people's code looks when you are reading it. – f1lt3r Jun 21 '17 at 06:43
  • @f1lt3r I disagree. If people really want to view my code with 8 spaces more power to them. I don't want to restrict them that way just so I can view it with 4 spaces on github for myself. If the answer is going to change, it should be mortenpi's answer – Assimilater Mar 20 '18 at 03:40
  • @Assimilater - rofrol's answer wouldn't restrict anyone from viewing whatever width they want. Agree that mortenpi's answer is good though pretty frustrating to have to add the param for every file you look at. – f1lt3r Mar 21 '18 at 04:43
  • 9
    Can anyone explain the logic behind using 8 spaces as a default? I can't imagine any scenario where 8 spaces would look anything other than ridiculous - yet it's the default on github? What gives? – PandaWood Jun 26 '18 at 05:50
  • Eight space tabs was the Unix default many moons ago. There's probably a ton of old C on GitHub that assumes it. – Denis Howe Aug 16 '19 at 09:45
  • Indent size can be different then tab size. Most editors can still offer an indent size of 4, with a tab size of 8. See this post: https://terminatorul.wixsite.com/blog/post/tabs-and-tab-size – Toughy Jun 12 '21 at 01:23
  • 4
    Sept. 2021: you now have an official GitHub profile setting, which will apply to all GitHub pages. See [my answer below](https://stackoverflow.com/a/69271299/6309). – VonC Sep 21 '21 at 15:07

10 Answers10

412

You can append ?ts=2 or ?ts=4 to the URL to change the tab-size.

Example: https://github.com/jquery/jquery/blob/main/src/core.js?ts=2

It seems that the value can be anything from 1 to 12. It does not work on Gists or raw file views though.

Source: GitHub Cheat Sheet

McNets
  • 10,352
  • 3
  • 32
  • 61
mortenpi
  • 4,351
  • 2
  • 18
  • 20
  • 121
    It's good that this is possible, but it would be nice if there was an easy way to choose the tab width rather than having to remember the URL parameter. – aross May 19 '14 at 09:20
  • 110
    It would also be nice if github let you save that as a preference so you wouldn't need to keep putting it back in the URL. – FrustratedWithFormsDesigner Jul 21 '16 at 17:15
  • This seems to be no longer working. When viewing the example given above (https://github.com/jquery/jquery/blob/master/src/core.js?ts=2) the tab size displays as 8, not 2. – Phil Dennis Apr 05 '17 at 14:37
  • 5
    @PhilDennis Works for me with Chrome (on Linux). – mortenpi Apr 06 '17 at 04:47
  • @NikolaMihajlović What do you mean by "in commits"? It appears to be working in the commit view (https://github.com/jquery/jquery/commit/1ea092a54b00aa4d902f4e22ada3854d195d4a18?ts=12) on Chrome/Linux. – mortenpi Apr 27 '18 at 20:56
  • @mortenpi Super weird, it seems it is selective about extensions e.g. doesn't work for json: https://github.com/jquery/jquery/commit/6acf4a79467a5aea5bc1eb7d552d72366718635d#diff-df39304d828831c44a2b9f38cd45289c?ts=2 – Nikola Mihajlović Apr 28 '18 at 21:50
  • 1
    @NikolaMihajlović It's a bit subtle, but the ts argument needs to be before the # fragment in the URL. E.g. https://github.com/jquery/jquery/commit/6acf4a79467a5aea5bc1eb7d552d72366718635d?ts=4#diff-df39304d828831c44a2b9f38cd45289c – mortenpi May 05 '18 at 22:07
  • 8
    nice solution. sadly doesn't work on diffs in PR's. – bbjay Apr 07 '19 at 21:52
  • 2
    in firefox you can use trumpio's addon _request control_ for creating rules. i apply the `ts` query parameter to every request to *.github.com. – jg6 Jun 11 '20 at 21:13
  • @bbjay it does work for diffs in pull requests now. – T3rm1 Jul 09 '20 at 12:21
  • @T3rm1 , the normal commit view or Files changed view in PRs seems not to handle `?ts=4` so far. What is the trick? – kxr Feb 26 '21 at 18:31
  • This doesn't seem to work anymore in 2021. GitHub automatically removes this from the URL. The example URL you gave ends up displaying 8-wide tabs. – Aaron Franke Sep 12 '21 at 15:45
  • You can now set tab width in github.com/settings/appearance (see VonC's answer below). – user673679 Dec 28 '21 at 11:17
  • It's weird that this tops out at 12 spaces. I really want 24 spaces for viewing assembly listings in wide column format. The `?ts=` query param changes the `data-tab-size="12"` HTML attribute on the table, which in turn changes the `tab-size` css value. The `data-tab-size` attribute only accepts the values 1-12. However, you can set the `tab-size` css value to anything and it works fine. For some reason there an indirect lookup between the data tribute and the actual CSS value that only respects tab spacing 1-12 and it's an annoying restriction. – Ryan Jan 06 '23 at 06:15
337

Set default displayed tab size for your repository

When you have a .editorconfig in your repository it will respect it when viewing code on GitHub.

indent_style = tab and indent_size = 4 shows tabs with 4 columns instead of 8 https://github.com/isaacs/github/issues/170#issuecomment-150489692

Example .editorconfig for multiple extensions which works in JetBrains' products:

root = true

[*]
end_of_line = lf
insert_final_newline = true

# Matches multiple files with brace expansion notation
[*.{js,jsx,html,sass}]
charset = utf-8
indent_style = tab
indent_size = 4
trim_trailing_whitespace = true

[*.md]
trim_trailing_whitespace = false

Change how you see tabs on other repositories

Install Stylus in your browser, than install GitHub: better-sized tabs in code.

There are also Google Chrome extensions:

rofrol
  • 14,438
  • 7
  • 79
  • 77
  • 2
    It seems that github does not respect the editorconfig file for files with no names (.gitconfig, etc). Any idea why or is it a bug? Ex https://github.com/rmandvikar/git-setup/blob/tabs/.gitconfig – hIpPy Apr 19 '17 at 05:54
  • 8
    dotfiles don't seem to be respected with the `[*]` (on github). I had to add another entry with `[.*]`. – PotatoFarmer Sep 25 '17 at 15:28
  • This should be the accepted answer by far! I'm amazed that Github actually does follow editor config rules. – Maurício Giordano Oct 25 '17 at 05:12
  • 2
    It's not respected in commits :-( – Nikola Mihajlović Apr 26 '18 at 19:43
  • It fixes the size of a tab in your editor too, though, which kind of defeats the whole purpose of tabs. – Caleb Hearon Sep 12 '18 at 19:00
  • 1
    Has this feature been removed from github? Some of my previously correct repo's seem to be reset to 8 spaces. – Redsandro Oct 14 '19 at 01:24
  • @Redsandro Can you make a testing repo? – rofrol Oct 14 '19 at 08:46
  • 4
    @rofrol I think I was mistaken. The tab size works correctly in code and diffs, but not in `README.md` code snippets. This is a new observation; I don't know if `README.md` code snippets ever did tab sizes other than 8 spaces. – Redsandro Oct 14 '19 at 13:44
  • Use [Stylus](https://chrome.google.com/webstore/detail/stylus/clngdbkpkpeebahjckkjfobafhncgmne?hl=en), not Stylish. Stylish had spyware, and might again in the future. – easrng May 06 '20 at 01:05
  • @Perhapsyouseethisname. Updated. Thanks – rofrol May 06 '20 at 13:05
  • Unfortunately this only works on tab spacing from [1..12] which appears to be a fundamental limitation of the way that it's implemented on the page. – Ryan Jan 06 '23 at 06:22
95

Since Sept. 2021, you can set the tab size directly in your GitHub settings: github.com/settings/appearance.

Announced in Changelog "Tab size rendering preference".

Just:

setting

Note: you cannot enter "3" for instance. You would get:

Tab size rendering preference could not be saved:
Validation failed:
Tab size is not included in the list

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • 13
    This answer should be higher up; it's unfortunate that the outdated answers have more votes. I initially saw the top two answers (by votes), thought that it wasn't possible as a user-specific setting and gave up. Then I stumbled across this setting by accident in the UI. – typesanitizer Nov 22 '21 at 18:09
  • This doesn't replace .editorconfig. Different file formats need different indentation. That's kinda the point. – Ryan Jan 06 '23 at 06:23
  • @Ryan I agree. This is not a complete solution – VonC Jan 06 '23 at 06:56
72

It actually is possible to do it, with a browser extension. Install Stylish (in Firefox or Chrome), then install this user style: “GitHub: better-sized tabs in code”.

It might not work for some languages. For example, I was viewing a JavaScript file and I did not notice any changes. So I deleted the style the author had and put the following lines into it:

.tab-size {
  -webkit-tab-size: 4 !important;
     -moz-tab-size: 4 !important;
       -o-tab-size: 4 !important;
          tab-size: 4 !important;
}

And it worked on Chrome (screenshot).

As you can see from the screenshot, I also enabled widescreen mode and changed the color scheme to Solarized. So I have three user styles running on GitHub pages via the Stylish extension for Chrome. I hope this helps someone.

DisgruntledGoat
  • 70,219
  • 68
  • 205
  • 290
aledujke
  • 1,125
  • 7
  • 15
  • 19
    I wrote [that user style](http://userstyles.org/styles/70979/github-better-sized-tabs-in-code). I’m glad you found it useful. I’ve fixed it and tested it in Chrome, and it now works without your modification. – Rory O'Kane Jun 22 '13 at 00:30
  • 2
    You might also like my user style “[All code has tab size 4](http://userstyles.org/styles/89425/all-code-has-tab-size-4)”, which changes the tab size of `` elements on all websites. – Rory O'Kane Jun 22 '13 at 01:07
  • Hey Rory! Thank you for that style. I'll check out the other one as well. Only I like my tab to be size 2 so I think I'll thinker it a bit :P – aledujke Jun 25 '13 at 08:34
  • Great tool. While I normally like a 2 space tab size, I noticed that setting it to two spaces hid some "white space anomalies" (lines with incorrect indentation) so I compromised and set stylish to 4. – Iain Jul 25 '13 at 11:45
  • 1
    Github replaces every `\t` with 8 ` `. Merde. – Rudie Dec 18 '13 at 17:46
  • 2
    Yep They did not do that before and I have no idea why they do that now :( I guess a solution can be someone writing a script that replaces say... 4 consecutive   with two or whatever. But that has to be a "userscript" I think. – aledujke Dec 19 '13 at 11:38
  • Maybe a browser extension that would use @mortenpi's solution to append the official supported URL argument to GitHub URLs would do the trick? – Bart Verkoeijen May 16 '14 at 02:46
  • @aledujke: Something like this: https://github.com/NV/tab_character_size Haven't tested as the site (userscripts.org) currently seems to be down. – aross May 19 '14 at 09:16
  • 1
    Our [GitHub Dark](https://userstyles.org/styles/37035/github-dark) Stylish userstyle allows you to set the tab size. And it is being actively maintained. – Mottie Jun 28 '14 at 18:24
  • This plugin definitely works but you need to tweak the selectors. A user helpfully provided a version in a [discussion post](https://forum.userstyles.org/discussion/46153/x) that works fine for me at time of writing. – Tom K Mar 15 '16 at 03:46
  • @RoryO'Kane The extension does not work on diff. Someone proposed a fix a while ago already. Could you apply it? – Ten Jan 27 '20 at 18:15
  • @Ten I just updated the extension so it works in diffs too. Thanks for letting me know about the bug. – Rory O'Kane Feb 18 '20 at 18:25
30

Update

Yes. As stated by mortenpi, this can be done by through an additional query parameter. See his answer for more details.

Original answer

Is that possible to change this configuration to 2 or 4 spaces?

No. It's only available as part of the editing feature through the Ace editor and the change is not persisted.

This blog post gives some more information about the embedded IDE.

However, provided you know the url of the blob (file) you're willing to review, you can switch to the edit mode easily by changing the blob segment with an edit segment and use the dropdown to select your prefered tab size.

tabSize

Community
  • 1
  • 1
nulltoken
  • 64,429
  • 20
  • 138
  • 130
  • 22
    Great idea, but the problem is once you go into EDIT mode, you also FORK the said archive. Might get a bit excessive after 50 or so view-only-edits ;) – tomByrer Jun 01 '12 at 07:32
  • 2
    Agreed. But that might be a good incentive to start *contributing* ;) – nulltoken Jun 01 '12 at 07:49
  • 14
    As @chrisdembia mentioned, this is no longer correct; github allows you to change tab size by passing the value as a query param (e.g., `?ts=4`) – dule Feb 02 '15 at 23:02
  • Is there a way for the community to override the selected answer? – chrisdembia Feb 04 '15 at 00:36
  • 1
    @chrisdembia Thanks for the reminders ;) Updated the answer to point to morenti's one. – nulltoken Feb 04 '15 at 06:56
10

If the project is yours, create a file in the project root named “.editorconfig” and give it the following contents.

[*]
indent_style = tab
indent_size = 4

This will cause GitHub to render tabs 4-wide within the project.

This is an EditorConfig file, which is formally specified, supported by many editors, and also supports more extensive editor configuration, like specifying that all .html files are UTF-8 encoded.

If the project isn’t yours, consider opening an issue requesting the author specify the indent style they intended.

twhb
  • 4,294
  • 2
  • 20
  • 23
  • 1
    `tab_width = 4` is more correct for specifying the rendering width of tabs, than `indent_size = 4` which specifies the default indentation width in an editor. – Ryan Jan 06 '23 at 06:27
  • @Ryan I agree, but I’d note that setting `indent_size` is what you want if 1 indent = 1 tab: `tab_width` defaults to `indent_size`, and you’d only want to break that link if you want e.g. 1 indent = 2 tabs, or to indent using spaces and use tabs elsewhere. – twhb Jan 14 '23 at 23:38
  • Ahh, right! That's good to know. – Ryan Jan 16 '23 at 02:32
0

If you're into UserScripts, this did it for me:

// ==UserScript==
// @name         GitHub Tabs
// @namespace    http://foldoc.org/
// @version      1
// @description  Set sensible tabs on GitHub
// @author       Denis Howe
// @match        https://github.com/*
// ==/UserScript==

document.querySelectorAll('table').forEach(t => { t.dataset.tabSize = 2 });
Denis Howe
  • 2,092
  • 1
  • 23
  • 25
  • I would have preferred this alternative but this seems to work more or less randomly: if the data was not loaded before the userscript runs (e.g. file listing then click to open the file), it doesn't work. – Ten Jan 27 '20 at 18:04
  • Good job, however, does not work for code blocks in the comment box. – 沙漠之子 Jul 02 '20 at 02:07
-4

I did that for fixing them http://valjok.blogspot.com/2014/07/indentation-correction-for-exposing.html.

Another option is when embedding your gist, replace all tabs with required number of spaces

<div id="willReplaceTabs">
 <script src="https://gist.github.com/valtih1978/99d8b320e59fcde634ad/cf1b512b79ca4182f619ed939755826c7f403c6f.js"></script>

 <script language="javascript">
  var spaces = "  "
  willReplaceTabs.innerHTML = willReplaceTabs.innerHTML.replace(/\t/g, spaces)
 </script>
</div>
Val
  • 1
  • 8
  • 40
  • 64
-8

If it's an option for the project you're working on, changing your editor to treat tabs as spaces will fix the problem.

So, for example, in Visual Studio Code, the config looks like this:

{
    "editor.tabSize": 2,
    "editor.insertSpaces": true
}

In Sublime it's:

{
    "tab_size": 2,
    "translate_tabs_to_spaces": true
}

Until recently I insisted on non-spaced tabs. After switching, it fixed the Github rendering weirdness, and I haven't noticed any significant downsides in my workflow.

Bryce Johnson
  • 6,689
  • 6
  • 40
  • 51
-33

The best solution is, if possible, to convince maintainers of the source code you're looking at to replace all the tabs by the correct number of spaces.

Using tabs is problematic in code today given that you're often seeing it on the web, where the decision of "how many spaces per tab" depends on where it's being displayed.

Tom Swirly
  • 2,740
  • 1
  • 28
  • 44
  • 3
    GitHub has a whitespace querystring var you can pass like `?w=1`. It'd be nice if they had a `?t=4' for 4 spaces or if you could somehow configure your project and it'd just pick it up, via Editor Config or something else. – jedmao Jun 30 '13 at 23:05
  • 7
    This is the correct answer and does not deserve to be downvoted. There's far too much software out there that doesn't let you change the tab width for "tabs are configurable" to be anything other than wishful thinking. And if you ever indent anything a distance that *isn't* a multiple of your preferred tab width, you now have a mixture of tabs and spaces and adjusting the tab size doesn't even work anymore. – zwol Jun 26 '14 at 14:57
  • 9
    Read the Wikipedia article about where the 8-space tab comes from. “A common horizontal tab size of eight characters evolved, despite five characters being half an inch and the typical paragraph indentation of the time, because as a power of two it was easier to calculate in binary for the limited digital electronics available.” Your answer uses circular reasoning (i.e. the standard is eight characters because that’s the standard) to shut the door on the question. The asker is not satisfied with this standard and has little reason to be. – Adam Jul 02 '14 at 15:22
  • 4
    @mrjedmao It does, you can do `?ts=4`. – Ben Aug 13 '14 at 15:35
  • 1
    @Ben thanks. I probably figured that out right after I posed this comment, because I've known about `?ts=4` for a while now and sindresorhus created a Chrome plugin for that, so bonus there! – jedmao Aug 13 '14 at 20:04
  • 8
    I prefer tab over space, because it speeds up my editing between 4 and 8 times when I move my cursor across a whitespace. –  Apr 30 '15 at 00:07
  • 1
    It should be noted that irregardless of whitespace preferences, there are times you are forced to use tabs such as Makefiles. Been dealing with how our Makefiles look horrible on GitHub by default since we use 4 space wide tabs. – mhoglan Jun 09 '15 at 15:11
  • 1
    "nobody can tell other developers how they should format their code. Nobody." Except the person who's paying the developer, hmm? "it speeds up my editing between 4 and 8 times when I move my cursor across a whitespace." Why do you do that to yourself, when all editors have a move-by-word/field/line feature? – Tom Swirly Jun 10 '15 at 20:10
  • 5
    "Except the person who's paying the developer, hmm?" Did your brain's IDE use its move-by-line feature to skip the following sentence? I explicitly stated that a project's enforced conventions take precedence over individual preferences. || "Why do you do that to yourself, when all editors have a move-by-word/field/line feature?" Yes, you're implying it's easier to use the feature of an editor to navigate code than to simply hit one key to traverse an entire column. Furthermore, not all editors have said features, and some even work differently. Stop thinking the whole world uses Sublime. –  Jun 23 '15 at 02:28
  • 1
    Sir, you have the correct answer. Looks like you got hated on pretty badly here! – Steven Shaw Aug 10 '17 at 01:50
  • 2
    If I quickly want to view a file in the browser that was intended to be viewed with a specific width of tabs (for alignment purposes), I'm not going to wait for months or potentially years for someone to change it. – byxor Oct 26 '17 at 15:27
  • 2
    Spaces or tabs is a whole different argument that you've successfully trolled. But your "answer" does not answer the question in any way. – Rupert Feb 19 '19 at 14:23
  • This “solution” sounds like communism. “Just do the same because your opinion doesn’t matter.” – Franklin Yu Nov 23 '19 at 06:54