1

When I need to work with some scrambled javascript/json code, one of my first steps is to introduce proper line breaks, followed by proper brace based indentation. For the later, I found the following command very helpful:

:%g/{/:normal $>i{

With at most a single opening brace on any line, this produces near perfect indentation in no time.

However, when trying to run the same command from the command line with the -c option, it simply does not seem to have any effect. I tried the following invocation:

vim -c ':%g/{/:normal $>i{' -c ':wq' some_scrambled_json_file.json

Why does this not work? And how do I need to change my invocation to make it work?

  • This is not a direct answer to your question but I think it is worth mentioning: use the right tool for what you want to do. If you want to fix the indentation of a json file non interactively you can use python eg python -m json.tool bad.json > good.json it will save you the trouble to use vim for something it is not intended to do. (I said python because that's what I use but you can do the same with any other scripting language) – statox Jan 19 '18 at 13:23
  • @statox Yes, I know that there are other, probably better ways of achieving the same end. Nevertheless, this is an issue that I would not have expected or anticipated, and I'm quite curious to understand why the :g command behaves so much differently when invoked from the command line. So, I guess my main motivation for asking is, that I hope to become a better vim user that way... – cmaster Jan 19 '18 at 13:31
  • Of course your question is absolutely legit and I wasn't criticizing it :) I specified that my comment was not an answer but just something I felt worth mentioning. About your question, I just tried your command and it seems to be working pretty well for me actually (in a cygwin shell) – statox Jan 19 '18 at 13:38
  • 1
    For what its worth here is the result on a debian box of mine with vim 8.0... I have no idea what can cause your problem... – statox Jan 19 '18 at 14:20
  • 2
    You can always try to start vim without a vimrc (-U NONE) if it works well you can check how to debug my vimrc? And yes, wait a bit before deleting your question some people here are amazing vim experts, they might come up with a solution. Happy vimming :) – statox Jan 19 '18 at 14:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.