In vim, the default indentation for JSON is:
{
\"employees\": [
{ \"firstName\":\"John\" , \"lastName\":\"Doe\" },
{ \"firstName\":\"Anna\" , \"
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
.