转载于EditorConfig官网 。
前言
EditorConfig工具有助于跨各种编辑器和IDE从事同一项目的多个开发人员维护一致的编码风格。
官网:链接 。
模板
.editorconfig
文件模板,存放于项目根目录:
# 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支持,无需下载安装该插件,部分IDE需下载插件进行支持,详情请参见官网。以下使用说明摘自官网。
配置文件存放位置:
.editorconfig
配置文件存放于项目根目录。
当打开一个文件时,EditorConfig插件会在打开文件的目录和每个父目录中查找一个名为.editorconfig
的文件。如果找到根文件路径,或者找到一个root=true
的.editorconfig
文件,那么对.editorconfig
文件的搜索将停止。
匹配原则:
* 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
关键字释义:
indent_style
: set to tab or space to use hard tabs or soft tabs respectively.
indent_size
: a whole number defining the number of columns used for each indentation level and the width of soft tabs (when supported). When set to tab, the value of tab_width
(if specified) will be used.
tab_width
: a whole number defining the number of columns used to represent a tab character. This defaults to the value of indent_size
and doesn’t usually need to be specified.
end_of_line
: set to lf
, cr
, or crlf
to control how line breaks are represented.
charset
: set to latin1
, utf-8
, utf-8-bom
, utf-16be
or utf-16le
to control the character set.
trim_trailing_whitespace
: set to true to remove any whitespace characters preceding newline characters and false to ensure it doesn’t.
insert_final_newline
: set to true to ensure file ends with a newline when saving and false to ensure it doesn’t.
root
: special property that should be specified at the top of the file outside of any sections. Set to true to stop .editorconfig
files search on current file.