My personal case

I’ve been using many different tools in my software development career. Some of them are in my belt due to they are unavoidable, it’s mostly about an official SDK or other development software from a provider. Another part of them is about "the best tool for the task", e.g. JetBrains IDEA pops up when we talk about Java related development, an epoch before that Eclipse was "The One", Xcode is for Apple platform, or we tend to run a Jupyter Notebook for data analysis, and so on. But I have tools that are always with me as supplementary ones or even as a main drill. One such tool is Vim.

I had a case working with compacted JSON content: REST request-response and related activity. And it was about working not only on my usual dev box, where I have all the tools configured according to my preferences and ready for action, but also on remote Unix-like machines. Usually, in my experience, a remote host has some version of Vim around, or it’s very easy to get it installed with a built-in package manager. I guess, when we work on a remote machine for a long time compared to occasional fact-checking and administration, we usually end up with some basic tools installed, and vi/Vim is quite a frequent choice.

So, I had to work with JSON in Vim and very quickly I ended up with a wish to get it formatted automatically, i.e. convert it from a minimized variant to a human readable one with respective indentation and spacing. And it’s turned out in my case that a remote host already has minimal Vim and Python. And I remember that Python has built-in json module. Having Vim and Python already installed was ideal for my situation not to alter a host’s state via package management, even if I prefer to use jq or a similar more sophisticated tool. Anyway, in my situation I needed formatting only.

Examples

The whole file can be formatted like this:

:%!python -m json.tool

Or a selected part:

:'<,'>!python -m json.tool

The history of Python is a separate epic story with its mysteries and myths, so some hosts make me use python3 instead of just python. Not a big deal.

 
 


Copyright © Igor Ostapenko
(handmade content)

Submit a like
 
Post a comment