How to fix JSON indentation in vim?
Easier way is to just external command as a filter for a selection. e.g.
- Make a selection
- Type
:!python -m json.tool
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 <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: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
.
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 -m json.tool