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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 05:22:58  来源:igfitidea点击:

How to fix JSON indentation in vim?

javascriptjsonvim

提问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.

更简单的方法是将外部命令作为选择的过滤器。例如

  1. Make a selection
  2. Type :!python -m json.tool
  1. 进行选择
  2. 类型 :!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 jsonfiletype. I use this nice command:

romainl 建议是首选方式,但有时您需要在一些没有json文件类型的缓冲区中对 JSON 文本进行相当缩进。我使用这个不错的命令:

command! -range -nargs=0 -bar JsonTool <line1>,<line2>!python -m json.tool

Just run :JsonTooland 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: ajand ijas well as print print JSON formatting, e.g. gqaj.

如果您没有 python 或者更喜欢纯 vim 解决方案,您可能会对 Tim Pope 的jdaddy插件感兴趣。Jdaddy提供JSON文本对象:ajij以及打印打印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.toolreorders 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-jin .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=Gis what you need if you are using vim.

gg=G如果您使用 vim,这就是您所需要的。

回答by gabra

If you have jq(source) available, you can use in the command mode:

如果您有jq( source) 可用,则可以在命令模式下使用:

:%!jq .

回答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)