使用editorconfig配置你的编辑器

简介: 使用editorconfig配置你的编辑器

摘要:


  

在团队开发中,统一的代码格式是必要的。但是不同开发人员使用的编辑工具可能不同,这样就造成代码的differ。今天给大家分享一个很好的方法来使不同的编辑器保持一样的风格。

  

不同的编辑器也有设置代码风格的,例如我们前端人员最喜欢使用的sublime text 2编辑器。你可以在preferences中选择Settings-User,然后输入自定义的风格,比如:


{
  "default_line_ending": "unix",
  "font_size": 14,
  "ignored_packages":
  [
    "Vintage"
  ],
  "tab_size": 4,
  "translate_tabs_to_spaces": true
}


今天我们用editorconfig来实现多种编辑器的代码风格统一。


简介:


  

EditorConfig帮助开发人员定义和维护一致的编码风格在不同的编辑器和IDE。


EditorConfig项目包含一个文件格式定义编码风格和文本编辑器插件的集合。


EditorConfig文件易于阅读并且他们与版本控制器很好地合作。


image.png

editorconfig文件:


  

下面是一个editorconfig文件例子,用于设置Python和JavaScript行尾和缩进风格的配置文件。


# EditorConfig is awesome: http://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)
[*.js]
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


点击此处寻找更多的实例:


怎样安装editorconfig文件:


  

当打开一个文件时,EditorConfig插件会在打开文件的目录和其每一级父目录查找.editorconfig文件,直到有一个配置文件root=true。EditorConfig配置文件从上往下读取,并且路径最近的文件最后被读取。匹配的配置属性按照属性应用在代码上,所以最接近代码文件的属性优先级最高。


注意:Windows 用户在项目根目录创建.editorconfig文件,可以先创建“.editorconfig.”文件,系统会自动重名为.editorconfig。


文件格式:


  

EditorConfig文件使用INI格式。斜杠(/)作为路径分隔符,#或者;作为注释。EditorConfig文件使用UTF-8格式、CRLF或LF作为换行符。


通配符:


* 匹配除/之外的任意字符串
** 匹配任意字符串
? 匹配任意单个字符
[name] 匹配name字符
[!name] 匹配非name字符
{s1,s2,s3} 匹配任意给定的字符串(since 0.11.0)


属性:

  

indent_style: 设置缩进风格,tab或者空格。tab是hard tabs,space为soft tabs。

  

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表明使文件以一个空白行结尾

  

root: 表明是最顶层的配置文件,发现设为true时,才会停止查找.editorconfig文件。

 

点击此处查看更多属性


注意:属性不区分大小写


实例:


  

下面以sublime text为例,测试editorconfig是否起作用。首先需要给sublime安装EditorConfig插件,然后在项目的根目录新建文件".editorconfig",内容如下:


# EditorConfig is awesome: <a onclick="javascript:pageTracker._trackPageview('/outgoing/EditorConfig.org');" href="http://EditorConfig.org">http://EditorConfig.org</a>
# 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
# Tab indentation (no size specified)
[*.js]
indent_style = tab
tab_width = 50


相关文章
|
1月前
|
开发框架 JavaScript 前端开发
百度富文本编辑器配置(vue3)
百度富文本编辑器配置(vue3)
|
1月前
|
Unix Shell Linux
在 Linux 上把 Vim 配置为默认编辑器
在 Linux 上把 Vim 配置为默认编辑器
|
1月前
|
前端开发 数据可视化 API
前端react 18.2整合ckeditor富文本编辑器——配置插件、自定义toolbar工具栏(二)
前端react 18.2整合ckeditor富文本编辑器——配置插件、自定义toolbar工具栏
65 0
前端react 18.2整合ckeditor富文本编辑器——配置插件、自定义toolbar工具栏(二)
|
1月前
|
前端开发 JavaScript CDN
前端react 18.2整合ckeditor富文本编辑器——配置插件、自定义toolbar工具栏(一)
前端react 18.2整合ckeditor富文本编辑器——配置插件、自定义toolbar工具栏
58 0
|
1月前
|
Linux 开发工具 C++
Linux编辑器vim(含vim的配置)
Linux编辑器vim(含vim的配置)
63 0
|
1月前
|
Java
【学习记录】IDEA编辑器 - 类、方法模板配置
【学习记录】IDEA编辑器 - 类、方法模板配置
53 0
【学习记录】IDEA编辑器 - 类、方法模板配置
|
1月前
|
数据可视化 Linux 开发工具
《Linux从练气到飞升》No.04 Linux编辑器:vim的使用和一键配置
《Linux从练气到飞升》No.04 Linux编辑器:vim的使用和一键配置
51 0
|
1月前
|
存储 Linux 编译器
vim编辑器和gcc/g++编辑器的使用讲解
vim编辑器和gcc/g++编辑器的使用讲解
70 2
|
1月前
|
Linux 编译器 开发工具
Linux:详解(yum的使用、vim编辑器命令集合以及gcc/g++编译器的使用)
Linux:详解(yum的使用、vim编辑器命令集合以及gcc/g++编译器的使用)
140 1
|
1月前
|
Linux Shell 开发工具
【linux】Linux编辑器-vim
【linux】Linux编辑器-vim
72 0