editorconfig插件在Atom中必须配合项目根目录的.editorconfig文件才能生效,且其indent_size优先级高于Atom自身的tabLength设置;文件需以root=true开头、格式正确,否则插件静默不工作。

editorconfig 插件在 Atom 中不能只靠点几下设置就生效——它必须和 .editorconfig 文件配合,且优先级高于 Atom 自身的 tabLength 设置。否则你改了缩进宽度,保存时还是按 4 格缩进,或者粘贴代码后自动变成 tab。
安装 editorconfig 插件并确认启用
插件本身不提供 GUI 配置入口,装完即用,但容易被忽略的是:它必须读到项目根目录下的 .editorconfig 才会工作。
- 打开 Atom → Atom → Preferences → Install,搜索 editorconfig,安装官方维护的 editorconfig 插件(作者 sindresorhus)
- 安装后无需重启,但要确保插件状态是“Enabled”(右下角状态栏有时会显示 “EditorConfig: active”)
- 如果没反应,先检查项目根目录是否存在 .editorconfig 文件;不存在时插件完全静默,不会报错也不会提示
.editorconfig 文件必须放在项目根目录且格式正确
Atom 的 editorconfig 插件只向上查找最近的 .editorconfig,不会扫描子目录或全局路径。
- 文件名必须是 .editorconfig(开头带点,大小写敏感),不能叫 editorconfig 或 .editorconfig.json
- 必须以 root = true 开头,否则插件可能在子目录中继续向上查找,导致规则被意外覆盖
- 常见错误配置:indent_size = "2"(字符串值非法,应为 indent_size = 2)
- 示例最小可用配置:
[*]<br>root = true<br>indent_style = space<br>indent_size = 2<br>end_of_line = lf<br>charset = utf-8
缩进行为受三重控制,indent_size 优先级最高
很多人调了 Atom 设置里的 “Tab Length” 却发现没用,是因为 Atom 有三层缩进逻辑:
- editor.tabWidth(写在 ~/.atom/config.cson 中):仅影响光标移动、手动 Tab 键行为
- indent_size(写在 .editorconfig 中):决定保存、粘贴、自动缩进时的实际空格数,插件以此为准
- 如果同时启用了 prettier-atom,它的 prettier.tabWidth 会再覆盖 indent_size —— 此时得关掉 Prettier 的 “Use Prettier on save”,或让两者值一致
- 注意:tab_width 字段在 .editorconfig 中是无效的(EditorConfig 规范不认这个键),只能用 indent_size
与其他格式化工具共存时的冲突点
Atom 里常同时装 prettier-atom 和 linter-eslint,这时 editorconfig 插件只管基础缩进和换行,不管语义格式(如括号位置、分号)。容易出问题的场景:
- prettier-atom 默认开启 “Format on Save”,它会无视 .editorconfig 的 indent_size,只认自己配置里的 tabWidth
- linter-eslint 的 autofix 可能强行改缩进,和 editorconfig 规则打架
- 解决办法:
- 统一用
prettier-atom管理全部格式(关掉editorconfig插件也行,但失去跨编辑器兼容性) - 或保留
editorconfig,在prettier-atom设置里勾选 “Use EditorConfig”(如果插件支持) - 或禁用 Prettier 的自动格式,只用它做 lint 提示

















