Re: Json inline format

27 views
Skip to first unread message

Paul

unread,
May 7, 2022, 12:56:56 PM5/7/22
to vim_use
json_pp, or: perl -MJSON -0e 'print to_json decode_json(<>),{pretty=>1}'

Use {pretty=>1,canonical=>1} if you want sorted keys.

On Sat, May 07, 2022 at 04:01:55AM -0700, Ni Va wrote:
>Hi,
>
>How can i format inline json file to multines without python tool ?
>
>Thank you

Ni Va

unread,
May 7, 2022, 4:00:58 PM5/7/22
to vim_use
Thank you Paul, I don't want third part.
N

Marc Chantreux

unread,
May 7, 2022, 5:58:09 PM5/7/22
to vim...@googlegroups.com
> Thank you Paul, I don't want third part.

you mean you want a pure viml solution? what's the point?

regards,
marc

Ni Va

unread,
May 24, 2022, 9:50:40 AM5/24/22
to vim_use
Sorry Marc to answer quite times after..

I have solved my need by pure VimL autocommand and vim9 func.

        autocmd BufEnter *.json g:FormatJson()

def g:FormatJson() # {{{

    if ( line('$') == 1 ) # when json is not expanded
        silent! :%s/\("\w\+":\)\({\|\("[^"]\+"\(}*,\)\)\)/\1 \2/g
        silent! :%s/\(},\)/\1/g
        silent! :%s/\(^{\)/\1/g
        silent! :%s/\("[^"]\+"\)}/\1}/g
        silent! :%s/\(}\)}/\1}/g

        silent! norm gg=GG1G
    else
        # silent! norm ggVG1J just for debug
    endif
enddef # }}}


Reply all
Reply to author
Forward
0 new messages