javascript 如何在 vim 中修复 JSON 缩进?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16620835/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to fix JSON indentation in vim?
提问by user2309998
In vim, the default indentation for JSON is:
在 vim 中,JSON 的默认缩进是:
{
"employees": [
{ "firstName":"John" , "lastName":"Doe" },
{ "firstName":"Anna" , "lastName":"Smith" },
{ "firstName":"Peter" , "lastName":"Jones" }
]
}
But what I expect is:
但我期望的是:
{
"employees": [
{ "firstName":"John" , "lastName":"Doe" },
{ "firstName":"Anna" , "lastName":"Smith" },
{ "firstName":"Peter" , "lastName":"Jones" }
]
}
I did google and tried some vim-json plugins, but none of them fix this issue.
我做了谷歌并尝试了一些 vim-json 插件,但没有一个能解决这个问题。
回答by awesome_person
Easier way is to just external command as a filter for a selection. e.g.
更简单的方法是将外部命令作为选择的过滤器。例如
- Make a selection
- Type
:!python -m json.tool
- 进行选择
- 类型
:!python -m json.tool
回答by Peter Rincker
romainl recommendation is the preferred way, but sometimes you need to pretty indent JSON text inside some buffer that doesn't have the json
filetype. I use this nice command:
romainl 建议是首选方式,但有时您需要在一些没有json
文件类型的缓冲区中对 JSON 文本进行相当缩进。我使用这个不错的命令:
command! -range -nargs=0 -bar JsonTool <line1>,<line2>!python -m json.tool
Just run :JsonTool
and it will pretty print the current line. It can take a range as well:
只需运行:JsonTool
,它就会漂亮地打印当前行。它也可以采用一个范围:
:JsonTool
:'<,'>JsonTool
:10,25JsonTool
If you do not have python or prefer a pure vim solution you may be interested in Tim Pope's jdaddyplugin. Jdaddy provides JSON text objects: aj
and ij
as well as print print JSON formatting, e.g. gqaj
.
如果您没有 python 或者更喜欢纯 vim 解决方案,您可能会对 Tim Pope 的jdaddy插件感兴趣。Jdaddy提供JSON文本对象:aj
和ij
以及打印打印JSON格式,例如gqaj
。
回答by Reut Sharabani
You can send to an external tool, as an example, if you have python you can send the content to python's json tool using:
您可以发送到外部工具,例如,如果您有 python,则可以使用以下命令将内容发送到 python 的 json 工具:
:%!python -m json.tool
回答by Alfonso de la Osa
python -m json.tool
reorders the position of the JSON object properties, if you have node installed, you can just use this function:
python -m json.tool
重新排序 JSON 对象属性的位置,如果您安装了节点,则可以使用此功能:
function FormatJSON(...)
let code="\"
\ var i = process.stdin, d = '';
\ i.resume();
\ i.setEncoding('utf8');
\ i.on('data', function(data) { d += data; });
\ i.on('end', function() {
\ console.log(JSON.stringify(JSON.parse(d), null,
\ " . (a:0 ? a:1 ? a:1 : 2 : 2) . "));
\ });\""
execute "%! node -e " . code
endfunction
Mapped to f-j
in .vimrc
映射到f-j
.vimrc
nmap fj :<C-U>call FormatJSON(v:count)<CR>
You can also pass a number of spaces for a tab, 2 are the default if you don't specify any.
您还可以为选项卡传递多个空格,如果您未指定任何空格,则默认值为 2。
4fj
My complete .vimrc is here https://github.com/botverse/.dotfiles/blob/master/.vimrc
我完整的 .vimrc 在这里https://github.com/botverse/.dotfiles/blob/master/.vimrc
回答by DawnSong
gg=G
is what you need if you are using vim.
gg=G
如果您使用 vim,这就是您所需要的。
回答by gabra
回答by zinovyev
Here's an example in Ruby:
这是 Ruby 中的一个示例:
:%! ruby -rjson -e "print JSON.pretty_generate(JSON.parse(ARGF.read))"
(https://gist.github.com/zinovyev/c4b6ec3c24670278adfebfb9ecced84b)
( https://gist.github.com/zinovyev/c4b6ec3c24670278adfebfb9ecced84b)