美文网首页
EditorConfig使用指南

EditorConfig使用指南

作者: mudssky | 来源:发表于2021-06-17 17:54 被阅读0次

EditorConfig是用于统一不同IDE编码风格配置的一种配置文件.

容易阅读,并且方便版本控制系统管理,默认支持EditorConfig的IDE有很多

具体规范可以查看下面两个网址

https://editorconfig-specification.readthedocs.io/

https://editorconfig.org/

示例

下面是一个官网的例子,使用的配置文件是ini格式

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

# top-most EditorConfig file
root = true

# unix格式换行符,并且在文件末尾插入新行
[*]
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

文件匹配

字符模式 匹配内容
* 匹配任意字母组成的字符串,除了路径分隔符/
** 匹配任意字母组成的字符串
? 匹配任意单个字符
[name] 匹配括号内的任意单个字符
[!name] 匹配除了括号内的字符
{s1,s2,s3} 匹配其中任何一个,用逗号隔开的 (Available since EditorConfig Core 0.11.0)
{num1..num2} 匹配两个数字之间的整数

支持的配置项

EditorConfig文件部分包含由等号(=)分隔的键值对。 除root外,所有键值对都必须位于生效的部分下。

注意大小写敏感

Key Supported values
indent_style 使用tab 还是 space
indent_size 设置缩进宽度
tab_width 设置制表符宽度, 默认和indent_size 值一样,通常不需要设定
end_of_line 设置换行符 lf, cr, or crlf
charset 设置字符编码 latin1, utf-8, utf-8-bom, utf-16be or utf-16le . Use of utf-8-bom is discouraged.
trim_trailing_whitespace 设置为 true 删除所有换行符前的空白字符false 不删除
insert_final_newline 设置true 保存的时候文件结尾会生成新行 and false 不生成新行
root 需要在开头指定,设定为true以后搜索editorconfig到当前的文件就会停止

下面是我个人的配置,如果在vscode下使用默认是不支持的,需要安装一个插件

root = true

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

[*.md]
trim_trailing_whitespace = false

相关文章

网友评论

      本文标题:EditorConfig使用指南

      本文链接:https://www.haomeiwen.com/subject/cagcyltx.html