EditorConfig 是什么?

1,212 阅读2分钟

这是我参与更文挑战的第3天,活动详情查看:更文挑战

以前写的文章,继续分享。

顾名思义,EditorConfig就是编辑器配置,帮助开发人员在不同的编辑器和IDE之间定义和维护一致的编码样式,由用于定义编码样式的文件格式和一组文本编辑器插件组成,这些插件使编辑器能够读取文件格式并遵循定义的样式。EditorConfig文件易于阅读,并且与版本控制系统配合使用。

.editorconfig示例

下面是一个.editorconfig文件的示例,为Python和Javascript文件设置了行尾以及缩进的样式。

# EditorConfig is awesome: https://EditorConfig.org

# 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

当用IDE打开一个文件时,EditorConfig插件会在打开文件的目录和其每一级父节点查找.editorconfig文件,直到找到一个配置了root = true的配置文件。

文件格式详情

EditorConfig文件使用INI格式。斜杠(/)作为路径分隔符,#或者;作为注释。路径支持通配符:

通配符说明
*匹配除/之外的任意字符
**匹配任意字符串
?匹配任意单个字符
[name]匹配name字符
[!name]不匹配name字符
[s1,s2,s3]匹配给定的字符串
[num1..num2]匹配num1到mun2直接的整数

EditorConfig支持以下属性:

属性说明
indent_style缩进使用tab或者space
indent_size缩进为space时,缩进的字符数
tab_width缩进为tab时,缩进的宽度
end_of_line换行符的类型。lf, cr, crlf三种
charset文件的charset。有以下几种类型:latin1, utf-8, utf-8-bom, utf-16be, utf-16le
trim_trailing_whitespace是否将行尾空格自动删除
insert_final_newline是否使文件以一个空白行结尾
root表明是最顶层的配置文件,发现设为true时,才会停止查找.editorconfig文件

支持的编辑器及IDE

无需安装插件的

这些编辑器捆绑了对EditorConfig的原生支持。 _20181224144529

需要安装插件的

要将EditorConfig与其中一个编辑器一起使用,需要安装一个插件。 _20181224145127

要将EditorConfig与其中一个无头工具一起使用,也需要安装一个插件。 _20181224145019

总结:可以解决哪些问题?

  1. 解决markdown文件行尾空格自动删除的问题
# http://editorconfig.org
root = true

[*]
trim_trailing_whitespace = true

[*.md]
trim_trailing_whitespace = false
  1. 解决github代码展示或者是团队开发项目格式不统一的问题(有时候我本地的代码格式放在github上格式就会混乱,还是挺实用的)
# editorconfig.org
root = true

[*]
indent_size = 2
indent_style = space
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

更多

官网:editorconfig.org/ wiki文档:github.com/editorconfi…