How to auto format JSON on save in Vim

Ali picture Ali · Oct 6, 2014 · Viewed 28.8k times · Source

To be honest go has spoiled me. With go I got used to having a strict formatting standard that is being enforced by my editor (vim) and is almost accepted and followed by everybody else on the team and around the world.

I wanted to format JSON files on save the same way.

Question: How to auto format/indent/lint json files on save in vim.

Answer

Jose B picture Jose B · Oct 6, 2014

In one command, try this:

execute '%!python -m json.tool' | w  

You could then add you own key binding to make it a simpler keystroke. Of course, for this to work, you need to have Python installed on your machine.