Given a file with a large JSON object all on one line like


How can I format this to be human readable in Vim?


10 Answers 10


Or you can install jq which is faster (written in C) from your package manager (e.g. sudo apt install jq in Ubuntu/Debian, sudo dnf install jq on Fedora/RHEL/CentOS) or from source and then in vim, type:

:%!jq .
  • 1
    :%!jq works just as well for me. Commented Dec 27, 2019 at 22:38
  • 5
    Hm... only works with the added . for me. I wonder why. Commented Apr 15, 2020 at 23:02
  • 4
    On MacOS after installing jq with brew install jq I could also just type :%!jq without a dot at the end
    – olik79
    Commented Apr 20, 2021 at 19:01
  • 3
    Sometimes, when I search for how to do something inside vim, I find theses answers that reminds me that one of the reasons I love vim is because it works seamlessly with all command line tools we already have. Commented Jun 21, 2021 at 19:40
  • 3
    @Alexandre % stands for a range equal to 1,$ (the entire file) and ! filters the range of line to the external program jq.
    – SebMa
    Commented Apr 11, 2023 at 21:11

This one-liner works well to format JSON in Vim into a human readable form:

:%!python -m json.tool

  • 1
    Do you memorize that and type it every time or do you use some kind of alias?
    – asedsami
    Commented Dec 13, 2019 at 17:05
  • I tend to use the :%!jq . answer these days, as I do less with Python and tend to have jq installed on my systems for any JSON work in CLI. I also get notifications for this SO question quite often, so it helps me not to forget :P I prefer vim with minimal customization, no plugins, etc, which is another reason the jq solution is now my preferred.
    – ljs.dev
    Commented Dec 14, 2019 at 7:37
  • 8
    Just a warning that this will escape some characters into ASCII only. So if you want UTF8 encoding jq is the better option. Commented Feb 5, 2020 at 3:01
  • 4
    Python 3.9 has introduced the option --no-ensure-ascii.
    – Hotschke
    Commented May 27, 2021 at 6:55
  • 1
    Useful if you are on a cloud environment and don't have permissions to install jq
    – Anurag A S
    Commented Jul 20, 2022 at 15:11

To format in a deterministic way, we need to sort the hash. None of the other answers did that for me, so I created my own:

function! FormatJson()
python << EOF
import vim
import json
    buf = vim.current.buffer
    json_content = '\n'.join(buf[:])
    content = json.loads(json_content)
    sorted_content = json.dumps(content, indent=4, sort_keys=True)
    buf[:] = sorted_content.split('\n')
except Exception, e:
    print e


:call FormatJson()
  • 1
    Welcome to Vi and Vim! Nice first answer; it's interesting that you consider the sorting aspect!
    – D. Ben Knoble
    Commented Nov 19, 2019 at 15:26
  • 2
    About the sorting I'm pretty sure jq mentionned in SebMa answers has a --sort-keys option which would do the job. But it's cool to write your own implementation too :)
    – statox
    Commented Nov 19, 2019 at 16:23

I use this :Jsonf command. It's can format unicode.

command! Jsonf :execute '%!python -c "import json,sys,collections,re; sys.stdout.write(re.sub(r\"\\\u[0-9a-f]{4}\", lambda m:m.group().decode(\"unicode_escape\").encode(\"utf-8\"),json.dumps(json.load(sys.stdin, object_pairs_hook=collections.OrderedDict), indent=2)))"'

If PHP is available, add the tool jf as a JSON Formatter by Composer:

$ composer global require codegear/json-formatter

Then Format current file:

:%!jf %

Or add a keymap in vimrc:

nnoremap <Leader>jf :%!jf %<CR>

If nodejs and xargs are available on your system you can add the following command to your .vimrc

command! -range FormatJson <line1>,<line2>!xargs -0 -I {} node -e 'console.log(JSON.stringify({}, null, 2));'

After that you can visual select the JSON text in the buffer and run


to pretty print it. Works with Unicode too.


Another solution is to use coc-format-json.

  • 1
    Welcome to Vi and Vim! This answer would benefit from further explanation via edit.
    – D. Ben Knoble
    Commented Mar 9, 2021 at 14:42

I wanted to open jq with vim. However i didn't manage to find a way to do it in one command.

I wrote a shell script, and it worked for me (linux version : redhat 7.6):

# Don't miss the dot after jq
cat input.json | jq . > output.json
vim output.json
  • 5
    You can use - to read from stdin: jq input.json . | vim - (maybe you need to reverse the input.json and . in that jq command). Another option would be to use -c or + to run a command when Vim opens: vim +':%!jq .' input.json Commented May 3, 2021 at 7:41

Use ALE to help you with linting/formatting

  1. Install ALE into .vim/vimfiles/pack/*/opt/ale

  2. add the following line to your vimrc:

packadd ale
  1. create .vim/vimfiles/after/ftplugin/json.vim
if exists("b:my_ftplugin") | finish | endif
let b:my_ftplugin = 1
if !exists('g:loaded_ale') | finish | endif
" Pick from available fixers :help ale-fix
" E.g., use prettier if it is installed:
let b:ale_fixers = ['prettier']

If no fixers are available at your disposal, python is always an option:

if !executable('python') | finish | endif
let b:ale_fixers = [{ -> {'command': 'python -m json.tool --indent 2 -'}}]
  1. Run the following command to reformat the current vim buffer:

Only usable for one (or few) files.

Set all values at a new line with substitute commands (they must fit your file!):

  • :%s/","/",\r"/g
  • :%s/null,"/null,\r"/g
  • :%s/(\d),"/\1,\r"/g
  • ... (there are },{, [{

Then go to the first { and type =%.

The JSON will be rudimentary formatted. Not perfect but readable.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.