Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSON pretty format only of part of a file in vim

Tags:

json

vim

In VIm, is there a way to print a JSON snipped inside a file in the "pretty" format?

For example, having the following file

# a comment
def my_func():
    pass

{"bla": [1, 2, 3], "yes": false}  # <--- pretty print this

# another comment
<foo>why do I mix everything in one file?</foo>
<bar>it's an example, dude</bar>

I would like to change the marked line to

{
   "bla":[
      1,
      2,
      3
   ],
   "yes":false
}

I'm looking for something like :%!python -m json.tool but only for the selected lines.

like image 924
ezdazuzena Avatar asked Apr 12 '18 13:04

ezdazuzena


1 Answers

Specifying the line number should work. For example:

:5!python -m json.tool

Or if it takes multiple lines:

:4,6!python -m json.tool
like image 65
Marc Avatar answered Oct 10 '22 09:10

Marc