Specify a range for a python command alias

21 views
Skip to first unread message

Haslett, Garvin

unread,
Sep 22, 2021, 7:04:32 AM9/22/21
to vim...@googlegroups.com
Following the web page listed below I've created the following command alias in my .vimrc to conveniently prettify .json files:
command! Pj %!python -m json.tool

It works great but, ideally, I'd like to specify ranges. I tried the following:
command! -range=% Pj !python -m json.tool
But the command just hangs until such times as I Ctrl-C to abort.

I'm surprised by this since entering `3,4!python -m json.tool` at the command buffer works as expected.

What subtlety am I missing here?

Web page mentioned above:
https://pascalprecht.github.io/posts/pretty-print-json-in-vim

Jürgen Krämer

unread,
Sep 22, 2021, 9:28:08 AM9/22/21
to vim...@googlegroups.com
Hi,

Haslett, Garvin schrieb am 22.09.2021 um 13:04:
> Following the web page listed below I've created the following command alias in my .vimrc to conveniently prettify .json files:
> command! Pj %!python -m json.tool
>
> It works great but, ideally, I'd like to specify ranges. I tried the following:
> command! -range=% Pj !python -m json.tool
> But the command just hangs until such times as I Ctrl-C to abort.
>
> I'm surprised by this since entering `3,4!python -m json.tool` at the command buffer works as expected.
>
> What subtlety am I missing here?

you need to tell Vim where you want to insert the range into
the replacement text for the user-defined command. You do this
by including the escape sequence <range> in the replacement
text.

In your case

:command! -range=% Pj <range>!python -m json.tool

should do the trick.

Regards,
Jürgen

Reply all
Reply to author
Forward
0 new messages