如何在Vim中保存时自动格式化JSON

时间:2023-01-14 10:45:37

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.

老实说,去了我。随着go我习惯了一个严格的格式标准,由我的编辑(vim)强制执行,并且几乎被团队和世界各地的所有人接受和遵循。

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

我想以同样的方式保存JSON文件格式。

Question: How to auto format/indent/lint json files on save in vim.

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

7 个解决方案

#1


38  

In one command, try this:

在一个命令中,试试这个:

execute '%!python -m json.tool' | w  

You could then add you own key binding to make it a simpler keystroke. Of course, for this to work, you need to have Python installed on your machine.

然后,您可以添加自己的键绑定,使其更简单的击键。当然,要实现这一点,您需要在您的计算机上安装Python。

#2


14  

If you are keen on using external tool and you are doing some work with json, I would suggest using the jq:

如果你热衷于使用外部工具并且你正在使用json做一些工作,我建议使用jq:

https://stedolan.github.io/jq/

https://stedolan.github.io/jq/

Then, you can execute :%!jq '.' inside vim which will replace the current buffer with the output of jq.

然后,您可以执行:%!jq'。'在vim内部,它将用jq的输出替换当前缓冲区。

#3


5  

Thanks mMontu and Jose B, this is what I ended up doing:

感谢mMontu和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).

警告这将覆盖您的缓冲区。因此,如果您打开已经存在语法错误的json文件,您将丢失整个文件(或者可能丢失它)。

Add this line to your ~/.vimrc

将此行添加到〜/ .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.

当然,你需要在你的机器上安装python。

EDIT: this next one should not overwrite your buffer if your json has error. Which makes it the correct answer, but since I don't have a good grasp of Vim script or shell for that matter, I present it as an experimental thing that you can try if you are feeling lucky. It may depend on your shell too. You are warned.

编辑:如果你的json有错误,下一个不应该覆盖你的缓冲区。这使得它成为正确的答案,但由于我没有很好地掌握Vim脚本或shell,我把它作为一个实验性的东西,你可以尝试,如果你感到幸运。它也可能取决于你的shell。你被警告了。

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

#4


3  

A search for JSON plugins on vim.org returned this:

在vim.org上搜索JSON插件返回了这个:

jdaddy.vim : JSON manipulation and pretty printing

jdaddy.vim:JSON操作和漂亮的打印

It has the following on description:

它有以下描述:

gqaj "pretty prints" (wraps/indents/sorts keys/otherwise cleans up) the JSON construct under the cursor.

gqaj“漂亮的打印”(包装/缩进/排序键/以其他方式清除)光标下的JSON构造。

If it does the formatting you are expecting then you could create an autocmd BufWritePre to format when saving.

如果它具有您期望的格式,那么您可以创建一个autocmd BufWritePre以在保存时进行格式化。

#5


2  

%!python -m json.tool

%!python -m json.tool

or

要么

%!python -c "import json, sys, collections; print json.dumps(json.load(sys.stdin, object_pairs_hook=collections.OrderedDict), ensure_ascii=False, indent=4)"

%!python -c“import json,sys,collections; print json.dumps(json.load(sys.stdin,object_pairs_hook = collections.OrderedDict),ensure_ascii = False,indent = 4)”

you can add this to your vimrc:

你可以将它添加到你的vimrc:

com! FormatJSON %!python -m json.tool

COM! FormatJSON%!python -m json.tool

than you can use :FormatJson format json files

比你可以使用:FormatJson格式的json文件

#6


1  

Vim Autoformat

Vim Autoformat

https://github.com/Chiel92/vim-autoformat

https://github.com/Chiel92/vim-autoformat

There is this Vim plugin which supports multiple auto format and indent schemes as well as extending with custom formatters per filetype.

这个Vim插件支持多种自动格式和缩进方案,并且每种文件类型都可以使用自定义格式化程序进行扩展。

https://github.com/Chiel92/vim-autoformat#default-formatprograms

https://github.com/Chiel92/vim-autoformat#default-formatprograms

Note:

注意:

You will need to have nodejs and js-beautify installed as vim-autoformat uses these as the default external tool.

您需要安装nodejs和js-beautify,因为vim-autoformat使用这些作为默认的外部工具。

npm install -g js-beautify

npm install -g js-beautify

#7


0  

you can search for 'vim-json-line-format' plugin, Open a file in Normal mode, move your cursor on the json line, use <leader>pj to show formated json by print it, use <leader>wj could change the text to formatted json. Invalid json can not format!

您可以搜索'vim-json-line-format'插件,在普通模式下打开文件,将光标移动到json线上,使用 pj通过打印显示格式化json,使用 wj可以更改格式为json的文本。无效的json无法格式化!

#1


38  

In one command, try this:

在一个命令中,试试这个:

execute '%!python -m json.tool' | w  

You could then add you own key binding to make it a simpler keystroke. Of course, for this to work, you need to have Python installed on your machine.

然后,您可以添加自己的键绑定,使其更简单的击键。当然,要实现这一点,您需要在您的计算机上安装Python。

#2


14  

If you are keen on using external tool and you are doing some work with json, I would suggest using the jq:

如果你热衷于使用外部工具并且你正在使用json做一些工作,我建议使用jq:

https://stedolan.github.io/jq/

https://stedolan.github.io/jq/

Then, you can execute :%!jq '.' inside vim which will replace the current buffer with the output of jq.

然后,您可以执行:%!jq'。'在vim内部,它将用jq的输出替换当前缓冲区。

#3


5  

Thanks mMontu and Jose B, this is what I ended up doing:

感谢mMontu和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).

警告这将覆盖您的缓冲区。因此,如果您打开已经存在语法错误的json文件,您将丢失整个文件(或者可能丢失它)。

Add this line to your ~/.vimrc

将此行添加到〜/ .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.

当然,你需要在你的机器上安装python。

EDIT: this next one should not overwrite your buffer if your json has error. Which makes it the correct answer, but since I don't have a good grasp of Vim script or shell for that matter, I present it as an experimental thing that you can try if you are feeling lucky. It may depend on your shell too. You are warned.

编辑:如果你的json有错误,下一个不应该覆盖你的缓冲区。这使得它成为正确的答案,但由于我没有很好地掌握Vim脚本或shell,我把它作为一个实验性的东西,你可以尝试,如果你感到幸运。它也可能取决于你的shell。你被警告了。

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

#4


3  

A search for JSON plugins on vim.org returned this:

在vim.org上搜索JSON插件返回了这个:

jdaddy.vim : JSON manipulation and pretty printing

jdaddy.vim:JSON操作和漂亮的打印

It has the following on description:

它有以下描述:

gqaj "pretty prints" (wraps/indents/sorts keys/otherwise cleans up) the JSON construct under the cursor.

gqaj“漂亮的打印”(包装/缩进/排序键/以其他方式清除)光标下的JSON构造。

If it does the formatting you are expecting then you could create an autocmd BufWritePre to format when saving.

如果它具有您期望的格式,那么您可以创建一个autocmd BufWritePre以在保存时进行格式化。

#5


2  

%!python -m json.tool

%!python -m json.tool

or

要么

%!python -c "import json, sys, collections; print json.dumps(json.load(sys.stdin, object_pairs_hook=collections.OrderedDict), ensure_ascii=False, indent=4)"

%!python -c“import json,sys,collections; print json.dumps(json.load(sys.stdin,object_pairs_hook = collections.OrderedDict),ensure_ascii = False,indent = 4)”

you can add this to your vimrc:

你可以将它添加到你的vimrc:

com! FormatJSON %!python -m json.tool

COM! FormatJSON%!python -m json.tool

than you can use :FormatJson format json files

比你可以使用:FormatJson格式的json文件

#6


1  

Vim Autoformat

Vim Autoformat

https://github.com/Chiel92/vim-autoformat

https://github.com/Chiel92/vim-autoformat

There is this Vim plugin which supports multiple auto format and indent schemes as well as extending with custom formatters per filetype.

这个Vim插件支持多种自动格式和缩进方案,并且每种文件类型都可以使用自定义格式化程序进行扩展。

https://github.com/Chiel92/vim-autoformat#default-formatprograms

https://github.com/Chiel92/vim-autoformat#default-formatprograms

Note:

注意:

You will need to have nodejs and js-beautify installed as vim-autoformat uses these as the default external tool.

您需要安装nodejs和js-beautify,因为vim-autoformat使用这些作为默认的外部工具。

npm install -g js-beautify

npm install -g js-beautify

#7


0  

you can search for 'vim-json-line-format' plugin, Open a file in Normal mode, move your cursor on the json line, use <leader>pj to show formated json by print it, use <leader>wj could change the text to formatted json. Invalid json can not format!

您可以搜索'vim-json-line-format'插件,在普通模式下打开文件,将光标移动到json线上,使用 pj通过打印显示格式化json,使用 wj可以更改格式为json的文本。无效的json无法格式化!