- 概述:editorConfig是一个名称为.editorconfig的自定义文件。该文件用来定义项目的编码规范,编辑器的行为会与.editorconfig 文件中定义的一致,并且其优先级比编辑器自身的设置要高,这在多人合作开发项目时十分有用而且必要.
- 安装:有些编辑器默认支持editorConfig,如webstorm;而有些编辑器则需要安装editorConfig插件,如ATOM、Sublime、VS Code等
- 启动:当打开一个文件时,EditorConfig插件会在打开文件的目录和其每一级父目录查找.editorconfig文件,直到有一个配置文件root=true
- 注意:
- EditorConfig的配置文件是从上往下读取的并且最近的EditorConfig配置文件会被最先读取. 匹配EditorConfig配置文件中的配置项会按照读取顺序被应用, 所以最近的配置文件中的配置项拥有优先权
- 如果.editorconfig文件没有进行某些配置,则使用编辑器默认的设置
语法:
通配符 | 功能 |
---|---|
* | 匹配除/之外的任意字符串 |
** | 匹配任意字符串 |
? | 匹配任意单个字符 |
[name] | 匹配name中的任意一个单一字符 |
[!name] | 匹配不存在name中的任意一个单一字符 |
{s1,s2,s3} | 匹配给定的字符串中的任意一个(用逗号分隔) |
{num1..num2} | 匹配num1到num2之间的任意一个整数,这里的num1和num2可以为正整数也可以为负整数 |
属性 | 功能 |
---|---|
indent_style | 设置缩进风格(tab是硬缩进,space为软缩进) |
indent_size | 用一个整数定义的列数来设置缩进的宽度,如果indent_style为tab,则此属性默认为tab_width |
tab_width | 用一个整数来设置tab缩进的列数。默认是indent_size |
end_of_line | 设置换行符,值为lf、cr和crlf |
charset | 设置编码,值为latin1、utf-8、utf-8-bom、utf-16be和utf-16le,不建议使用utf-8-bom |
trim_trailing_whitespace | 设为true表示会去除换行行首的任意空白字符。 |
insert_final_newline | 设为true表示使文件以一个空白行结尾 |
spaces_around_operators | 运算和布尔运算符周边是否需要空格 |
root | 表示是最顶层的配置文件,发现设为true时,才会停止查找.editorconfig文件,使用当前的文件配置 |
例子:
# 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,html,css}]
charset = utf-8
# 4 space indentation
[*.js]
indent_style = tab
indent_size = 4
# Matches the exact files either package.json
[{package.json}]
indent_style = space
indent_size = 2