How to fix JSON indentation in vim?

前端 未结 8 1327
粉色の甜心
粉色の甜心 2020-12-29 03:55

In vim, the default indentation for JSON is:

{
    \"employees\": [
    { \"firstName\":\"John\" , \"lastName\":\"Doe\" }, 
    { \"firstName\":\"Anna\" , \"         


        
8条回答
  •  北海茫月
    2020-12-29 04:27

    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:

    command! -range -nargs=0 -bar JsonTool ,!python -m json.tool
    

    Just run :JsonTool and it will pretty print the current line. It can take a range as well:

    :JsonTool
    :'<,'>JsonTool
    :10,25JsonTool
    

    If you do not have python or prefer a pure vim solution you may be interested in Tim Pope's jdaddy plugin. Jdaddy provides JSON text objects: aj and ij as well as print print JSON formatting, e.g. gqaj.

提交回复
热议问题