简体   繁体   中英

json.tool shell returned 1 and I lost my vim buffer

So I'm happily "hacking" away in the command line using vim. I'm working on some JSON text, I'm deleting objects, trimming down an API response to isolate a snippet to work more closely with.

At some point I'm pretty happy I've cut out all of the rubbish from the JSON and I've only got what I need to carry on working...

So I go ahead and type in :%!python -m json.tool and boom.

So my JSON text wasn't a valid JSON text after I poked around with it, looks like I forgot a comma somewhere.

:%!python -m json.tool
shell returned 1
Press ENTER or type command to continue

So I hit enter or any other key and then my buffer is replaced with a nice error message...

Expecting , delimiter: line 3 column 5 (char 20)

Oh so I did forget a comma, on line 3, thanks!

Oh but where's my JSON text gone? And that ladies and gentlemen is what I seek help with. This is usually where I :q! and reopen the JSON file and try to get it right this time. But surely I must be able to recover my buffer from just BEFORE I had the audacity to try and use the python json tool to format a blob of non-JSON text!

You can set 'makeprg' and 'errorformat' so you can just run :make to do linting .

autocmd FileType json setlocal makeprg=python\ -mjson.tool\ 2>&1\ %\ >\ /dev/null
                     \| setlocal errorformat=%m:\ line\ %l\ column\ %c\ %.%#

However, if you are like me and your json buffers are usually scratch buffers then use setqflist() to populate the quickfix list.

call setqflist([], ' ', {'efm':'%m: line %l column %c %.%#', 'lines': systemlist('python -mjson.tool', getline(1, '$'))})

For more help see:

:h 'makeprg'
:h 'errorformat'
:h setqflist()
:h systemlist()

If you like undo then you'll LOVE this one. The earlier command. Type :ea and then the number of seconds you want to go back in time and hit enter. The opposite of this is the later command, :lat .

However I must warn you that this time is only approximate and it is recommended that you leave insert mode as often as possible . It's generally easier to use undo and redo.

Vim 'earlier' and 'later' commands are not working as expected

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM