Skip to content

参考

基于 EditorConfig 规范编写

速查

  • 文件:项目内任意目录的全小写 .editorconfig;顶层加 root = true
  • 段:[glob];对:key = value;注释:#;(独占一行)
  • glob:*(不跨 /) / **(跨目录) / ? / [name] / [!name] / {a,b} / {n1..n2}
  • 通用属性:indent_style / indent_size / tab_width / end_of_line / charset / trim_trailing_whitespace / insert_final_newline
  • 取值大小写不敏感、统一小写;unset 取消任意属性
  • 优先级:越近的文件 / 越靠后的段,取胜

文件与语法

要素说明
文件名.editorconfig(全小写)
编码UTF-8,LFCRLF 换行
root置于文件顶部(任何段之外);true 停止向上查找
段(section)[glob],对其后属性生效,直到下一个段
键值对key = value
注释#;,独占一行
路径分隔符仅正斜杠 /

glob 通配符

模式含义
*任意字符,但不跨路径分隔符 /
**任意字符,可跨目录
?任意单个字符
[name]name 中的任一字符
[!name]不在 name 中的任一字符
{s1,s2,s3}给定字符串中的任一个(逗号分隔)
{num1..num2}num1num2 之间的任意整数
  • 段名不含 /:可在任意层级匹配。
  • 段名 /:相对于该 .editorconfig 所在目录。

通用属性

属性取值说明
indent_styletab / space硬 tab 或软 tab
indent_size整数 / tab缩进列数;tab 取 tab_width
tab_width整数tab 列数,默认取 indent_size
end_of_linelf / cr / crlf换行符
charsetlatin1 / utf-8 / utf-8-bom / utf-16be / utf-16le编码
trim_trailing_whitespacetrue / false删行尾空白
insert_final_newlinetrue / false末尾留换行
unset(值)任意属性取消该属性,回到编辑器默认

有限支持的属性

属性取值说明
max_line_length正整数 / unset硬换行行宽,仅部分编辑器/工具(Vim/Emacs/JetBrains/Prettier 等)支持

优先级规则

  • 多个 .editorconfig 自顶向下读取,越靠近目标文件的越后读、优先级越高。
  • 同一文件内,靠后的段覆盖靠前的段。
  • root = true 或文件系统根,停止向上查找。

相关链接