Vi and Vim Asked by oalders on August 31, 2021
I’ve got, in this case, a Perl module which has some JSON
snippets inside of it. I’d like to be able to select the embedded JSON
and format it via jq
. So far I’ve got:
:vnoremap jq <esc>:'<,'>:w !jq<CR>
That allows me to select some text, type jq
and then the formatted text is displayed to me. However, it’s not replacing the selected text. Nothing actually changes in my buffer. What am I missing here? I’ve seen lots of answers for how to format an entire file, but I can’t find a workable answer for this particular use case.
There at least two possible answers here:
vnoremap jq :!jq<cr>
as suggested by @Ralf works for jq
>= 1.6. See https://github.com/stedolan/jq/commit/5fe053671a768d28bc9773719cc4a9a35e72f30d
For earlier jq
versions where the dot is not implied, it needs to be in the command:
vnoremap jq :!jq .<cr>
I was using an older version of jq
. The answer which adds the dot will work with both versions.
If you'd like your keys to be sorted:
vnoremap jq :!jq -S .<cr>
Correct answer by oalders on August 31, 2021
This command will format and sort the json file
:%!jq -S .
Answered by H A on August 31, 2021
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP