I wrote simple jsonview
script to view json files:
#!/bin/bash
tmp_file=/tmp/jsonview.json
cat "${@}" | python -m json.tool > $tmp_file
[[ -f $tmp_file ]] && vim $tmp_file
I am not using less
because I need syntax highlighting.
That useless use of cat cat ${@} | ...
is so that script can be used as a filter:
jsonview t.json
and:
cat t.json | jsonview
If jsonview
used as in second, pipe case - despite the fact that vim is invoked not on pipe but on concrete file, I am getting that warning in subject. I can view json file, but after exit, it messes up terminal. Why is this warning? Why vim thinks that it reads from a pipe?
Vim doesn't like it when standard input is redirected unless you invoke it as vim -
. In that case it knows stdin is redirected and handles it. As a side benefit it also lets you get rid of the temp file.
#!/bin/bash
cat "$@" | python -m json.tool | vim +'set syntax=javascript' -R -
Always quote "$@"
to ensure file names with whitespace won't mess your script up.
-R
gets rid of the prompt to save the buffer when exiting Vim.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With