当我在GitHub上查看文件时,制表符显示为8个空格。

例子:

有可能把这个配置改为2或4个空格吗?


当前回答

如果你喜欢UserScripts,这对我来说很有用:

// ==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 });

其他回答

如果项目是你的,在项目根目录下创建一个名为“”的文件。Editorconfig”,并为其提供以下内容。

[*]
indent_style = tab
indent_size = 4

这将导致GitHub在项目中呈现4宽的选项卡。

这是一个正式指定的EditorConfig文件,许多编辑器都支持它,还支持更广泛的编辑器配置,比如指定所有.html文件都是UTF-8编码的。

如果项目不是你的,考虑打开一期,要求作者指定他们想要的缩进样式。

如果这是您正在处理的项目的一个选项,则将编辑器更改为将制表符视为空格将解决该问题。

例如,在Visual Studio Code中,配置是这样的:

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

在Sublime中是:

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

直到最近,我还坚持使用非空格制表符。切换后,它修复了Github渲染的怪异,我没有注意到我的工作流程中有任何显著的缺点。

为存储库设置默认显示的选项卡大小

当您的存储库中有.editorconfig时,它将尊重它 在GitHub上查看代码时。 Indent_style = TAB和indent_size = 4显示有4列的选项卡 而不是8 https://github.com/isaacs/github/issues/170#issuecomment-150489692

JetBrains产品中多个扩展的。editorconfig示例:

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

更改在其他存储库上查看选项卡的方式

在浏览器中安装Stylus,而不是安装GitHub:代码中更大的标签。

还有谷歌Chrome扩展:

https://chrome.google.com/webstore/detail/github-tab-sizer/djpnapkcpjomnkfekaedkigfomiedpjf https://chrome.google.com/webstore/detail/github-tab-size/ofjbgncegkdemndciafljngjbdpfmbkn/related

我这样做是为了修复它们http://valjok.blogspot.com/2014/07/indentation-correction-for-exposing.html。

另一种选择是在嵌入要点时,将所有制表符替换为所需数量的空格

<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>

您可以将?ts=2或?ts=4附加到URL以更改制表符大小。

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

这个值似乎可以是1到12之间的任何值。它不工作在gist或原始文件视图。

来源:GitHub Cheat Sheet