To be honest go has spoiled me. With go I got used to having a strict formatting standard that is being enforced by my editor (vim) and is almost accepted and followed by everybody else on the team and around the world.

I wanted to format JSON files on save the same way.

问:如何在保存到vim时自动格式化/缩进/lint json文件.

推荐答案

感谢Ammontu和Jose B,这就是我最终要做的:

WARNING this will overwrite your buffer. So if you OPEN a json file that already has a syntax error, you will lose your whole file (or can lose it).

将这一行添加到你的~/.vimrc

" Ali: to indent json files on save
autocmd FileType json autocmd BufWritePre <buffer> %!python -m json.tool

you need to have python on your machine, of course.

EDIT:如果json有错误,下一个不应该覆盖缓冲区.这使它成为了正确的答案,但由于我对Vim脚本或shell没有很好的理解,所以我将它作为一个实验性的东西,如果你觉得幸运的话,你可以try 一下.这可能也取决于你的shell .你被警告了.

" Ali: to indent json files on save
autocmd FileType json autocmd BufWritePre <buffer> %!python -m json.tool 2>/dev/null || echo <buffer>

Json相关问答推荐

JOLT将对象名作为新属性添加到主体中

在Jenkins中使用ReadJSON读取json子元素

在Ansible中从json中提取特定数据

在Zig中解析JSON失败

有没有办法让serde_json正确/不正确地处理NaN、inf和-inf(IEEE 754特殊标准)?

使用 jq 重新格式化 JSON 输出

Ansible - 将文件内容添加到字典中

Powershell解析JSON文件中的键或值

使用 serde 和 csv crates 将嵌套的 json 对象序列化为 csv

用于遮蔽卡的 Jolt 规格

Powershell JSON 操作

如何在 jQuery 中循环遍历 JSON 数组?

如何使用法语口音对数组进行 json_encode?

使用非美国日期格式时,JsonConvert.DeserializeObject 无法将字符串转换为 DateTime

对象序列化为 JSON(使用 Gson).如何在 UpperCamelCase 中设置字段名称?

PHP json_encode json_decode UTF-8

通过 JSON 发送 64 位值的公认方式是什么?

python追加到json对象中的数组

如何在 JAVA 中对 JSONArray 进行排序

Java HashMap 与 JSONObject