Format JSON in Vim

Considering you have jq installed, you can just call it within vim itself, passing current text as an argument:

:%!jq .

Or to make it permanent, add ,jf (json-format) shortcut to your .vimrc:

:nnoremap ,jf :%!jq .<CR>

Update 16/09/2021

You can also force the format of the file to be JSON after reformatting, for instance if you have just opened vim and pasted a JSON text, then reformatted. So instead of above use:

:nnoremap ,jf :%!jq .<CR> <bar> :set syntax=json<CR>


To contact me, send an email anytime or leave a comment below.