Running a command on a visual selection in Vim

As noted in Replacing Text in Vim with the Output of a Command, it can be handy to pipe a bit of text into another command.
For instance, we may have found a JSON Web Token (JWT), like the below:
{
"client_assertion": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c"
}
If we have a handy executable called jwt
, we'd want to create a visual selection inside the quotes around the JWT, and then:
Running
If you just want to just run the command, leaving the JWT in the buffer untouched, you can do the following:
y:echo system('jwt', @")
For example:
Replacing
If you just want to replace the JWT with the unpacked header and payload (if a JWS), you can do the following:
c<C-R>=system('jwt', @")<CR><ESC>
For example:
You may want to toggle :set paste
before and after, so the text doesn't get autoformatted by Vim when being inserted.