If you’ve ever tried to edit or view a json file that’s been generated from JSON or JSON:XS in vim, you know it’s not the easiest thing. Especially when the amount of content gets rather large.
Here’s how you can highlight and tidy json files from within vim.
Highlighting:
Download the json syntax highlighting file from vim.org and place it in your $HOME/.vim/syntax/ directory.
Place the following in your .vimrc file
au BufRead,BufNewFile *.json set filetype=json foldmethod=syntax
au! Syntax json source $HOME/.vim/syntax/json.vim
Tidy:
Install the perl module JSON::XS from cpan (if you don’t have it already)
sudo cpan JSON::XS
place the following in your .vimrc file
au FileType json command -range=% -nargs=* Tidy <line1>,<line2>! json_xs -f json -t json-pretty
Then whenever you need to tidify any json data you’ve opened in vim… just type
:Tidy






