推广

PolarisNet ¥9.9/月 Clash 订阅 · 稳定 · 低延迟 · 流媒体解锁 · 多设备 · 智能分流 · 7x24 监控

配置文件格式错误?

常见 YAML 语法踩坑列表

最后更新:2024年11月15日

典型报错:mapping values are not allowed here / did not find expected key / invalid character / Clash 启动后规则不生效。90% 来自缩进、冒号、列表或字符转义错误。

🧱 基础规则

  1. 只允许使用空格,禁止 Tab。(Tab 会被解释为不同宽度)
  2. 缩进层级通常为 2 空格或 4 空格,项目内保持统一。
  3. key: value 冒号后必须至少 1 个空格。
  4. 列表使用 - 后跟空格:- name
  5. 需要字符串原样保留时加引号:端口前导 0、布尔字样、含特殊字符。

🧪 常见错误对照

错误示例问题修正
port: 08被解析为 8(八进制风险)"08"
mixed-port:7890缺少冒号后空格mixed-port: 7890
- - name: test多余 -- name: test
cipher: [aes-128-gcm,aes-256-gcm]缺空格/逗号后空格cipher: [aes-128-gcm, aes-256-gcm]
`rules:
-DOMAIN,example.com,DIRECT`- 后缺空格- DOMAIN,example.com,DIRECT
password: abc:123冒号截断"abc:123"
#注释紧贴注释最好前留空格# 注释

🧩 列表与字典

正确:

proxies:
	- name: node1
		type: vmess
		server: x.x.x.x
		port: 443
		uuid: 1111-2222
	- name: node2
		type: hysteria2
		server: y.y.y.y
		port: 8443

错误(缩进错位):

proxies:
- name: node1
	 type: vmess   # 与上一行不对齐

🔐 引号策略

场景是否建议加引号示例
含冒号 / # / & / *必须"abc:123"
前导 0 / 全数字串建议"00123"
布尔字面量 true/false可避免误解析"false"
IPv6 地址建议"2606:4700::1111"
纯域名/普通词可不加example.com

🏷 锚点与引用

common-proxy: &base
	type: socks5
	server: 127.0.0.1
	port: 7890

proxies:
	- { name: local, <<: *base }

注意:<<: 合并只在字典层有效;列表不支持直接合并。

📜 多行与折叠

语法行为示例
``保留换行
>换行折叠为空格文本描述类字段

🛠 快速校验工具

  1. 命令行:yamllint config.yaml(需安装)。
  2. Clash 本身:查看启动日志第一屏是否报错。
  3. 在线验证:粘贴到 yamllint.com / codebeautify.org(注意脱敏)。

🧪 最小化测试法

怀疑错在某一段 → 复制文件 → 仅保留 port / proxies(1条) / rules(1条) → 若可启动,逐段恢复定位。

最小示例模板

port: 7890
socks-port: 7891
allow-lan: true
mode: rule
proxies:
	- name: test
		type: direct
		udp: true
proxy-groups:
	- name: 🚀 节点选择
		type: select
		proxies: [test]
rules:
	- MATCH,🚀 节点选择

🔁 终极排查 Checklist

  1. 查找 Tab:编辑器显示不可见字符 → 全部替换为空格。
  2. 逐层对齐:同级键垂直对齐。
  3. 注释行前留一个空格增强可读性。
  4. 复杂行(带逗号、冒号)先用引号包裹。
  5. 使用最小化测试法验证核心逻辑后再扩展。

遇到仍无法定位:附上报错信息 + 相关 20 行上下文(脱敏)求助,效率最高。

引用本页 Citation

复制以下任意格式以引用本页面内容。

APA
Clash版本库 Team. (2024). 配置文件格式错误?. Clash版本库 - 同步Github官方仓库版本. Retrieved from https://clash-version.com/faq/yaml-error/
MLA
"配置文件格式错误?." Clash版本库 - 同步Github官方仓库版本, 2024, https://clash-version.com/faq/yaml-error/.
BibTeX
@misc{ _,
  title={配置文件格式错误?},
  author={Clash版本库 Team},
  year={2024},
  howpublished={\url{ https://clash-version.com/faq/yaml-error/ }},
  note={Accessed: 2025-09-20}
}
JSON
{"accessed":"2025-09-20","author":"Clash版本库 Team","title":"配置文件格式错误?","url":"https://clash-version.com/faq/yaml-error/","year":"2024"}

内容有误?欢迎在 GitHub 提交修正建议

还有其他问题?

浏览全部常见问题