When I view files on GitHub, tabs appear as 8 spaces.
Example:
Is that possible to change this configuration to 2 or 4 spaces?
When I view files on GitHub, tabs appear as 8 spaces.
Example:
Is that possible to change this configuration to 2 or 4 spaces?
Yes. As stated by mortenpi, this can be done by through an additional query parameter. See his answer for more details.
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.
?ts=4
) –
Blubbery 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
ts
query parameter to every request to *.github.com. –
Gabar ?ts=4
so far. What is the trick? –
Antagonize ?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. –
Leukas 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
Install Stylus in your browser, than install GitHub: better-sized tabs in code.
There are also Google Chrome extensions:
[*]
(on github). I had to add another entry with [.*]
. –
Prospectus 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. –
Blacklist 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".
.editorconfig
?ts=2
added to the URLJust:
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
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.
<code>
elements on all websites. –
Tungting \t
with 8
. Merde. –
Serai pre { tab-size: 2 !important }
–
Iowa Yes. As stated by mortenpi, this can be done by through an additional query parameter. See his answer for more details.
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.
?ts=4
) –
Blubbery 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.
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. –
Leukas 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. –
Carbamate 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 });
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>
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.
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.
?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. –
Tharpe ?ts=4
. –
Patinated ?ts=4
for a while now and sindresorhus created a Chrome plugin for that, so bonus there! –
Tharpe © 2022 - 2024 — McMap. All rights reserved.
pre { tab-size: 4 }
– Ahmednagar.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. – Guib