json

Formatting JSON in Vim

You probably know that we can use something like :%!python -m json.tool to format a JSON file that’s opened in Vim. However, I always find this command hard to recall. Instead of dedicating a mapping for this, we can actually rely on equalprg instead. Let’s set this autocmd in .vimrc:

augroup vimrc
  autocmd!
  autocmd FileType json setlocal equalprg=python\ -m\ json.tool
augroup END

Now, when we open a JSON file, we can use the familiar = key to format it nicely.