EditorConfig 在支持他的编辑器中可以为项目中各种类型的文件单独设置缩进的策略,比如在 .go 文件中使用 tab 缩进,而在同项目的 .html 中适用两格空格缩进。

当然,这个需要编辑器的支持,Neovim 是支持的,这样在 neovim 的全局设置中可以设置为默认是四格空格缩进,而在项目中为 html, yaml 等文件单独设置为两格空格缩进,不需要手动切换。

使用的方法就是在项目的根目录放入一个 toml 格式的 .editorconfig 配置文件。

一个简单的例子

# content of .editorconfig file
 
# top-most EditorConfig file
root = true
 
# Unix-style newlines with a newline ending every file
[*]
end_of_line = lf
insert_final_newline = true
 
# Matches multiple files with brace expansion notation
# Set default charset
[*.{js,py}]
charset = utf-8
 
# 4 space indentation
[*.py]
indent_style = space
indent_size = 4
 
# Tab indentation (no size specified)
[Makefile]
indent_style = tab
 
# Indentation override for all JS under lib directory
[lib/**.js]
indent_style = space
indent_size = 2
 
# Matches the exact files either package.json or .travis.yml
[{package.json,.travis.yml}]
indent_style = space
indent_size = 2

文件匹配模式

文件中的 section name 支持模式匹配,有以下语法:

patterneffect
*Matches any string of characters, except path separators (/)
**Matches any string of characters
?Matches any single character
[name]Matches any single character in name
[!name]Matches any single character not in name
{s1,s2,s3}Matches any of the strings given (separated by commas)
(Available since EditorConfig Core 0.11.0)
{num1..num2}Matches any integer numbers between num1 and num2,
where num1 and num2 can be either positive or negative

支持的控制属性

KeySupported values
indent_styleSet to tab or space to use hard tabs or soft tabs respectively. The values are case insensitive.
indent_sizeSet to a whole number defining the number of columns used for each indentation level and the width of soft tabs (when supported). If this equals tab, the indent_size shall be set to the tab size, which should be tab_width (if specified); else, the tab size set by the editor. The values are case insensitive.
tab_widthSet to a whole number defining the number of columns used to represent a tab character. This defaults to the value of indent_size and should not usually need to be specified.
end_of_lineSet to lfcr, or crlf to control how line breaks are represented. The values are case insensitive.
charsetSet to latin1utf-8utf-8-bomutf-16be or utf-16le to control the character set. Use of utf-8-bom is discouraged.
spelling_languageSets the natural language that should be used for spell checking. Only one language can be specified. There is no default value.

The format is ss or ss-TT, where ss is an ISO 639 language code and TT is an ISO 3166 territory identifier.

Note: This property does not specify the charset to be used. The charset is in separate property charset.
trim_trailing_whitespaceSet to true to remove all whitespace characters preceding newline characters in the file and false to ensure it doesn’t.
insert_final_newlineSet to true ensure file ends with a newline when saving and false to ensure it doesn’t.
rootMust be specified in the preamble. Set to true to stop the .editorconfig file search on the current file. The value is case insensitive.

更完整的清单可以在这里找到。