Replacing Text in Vim with the Output of a Command
Let's say that we've got a buffer in Vim, and we want to replace some of the text with the output of a command.
For instance, we may have found a JSON Web Token (JWT), like the below:
eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c
If we have a handy executable called jwt
, we'd be able to select the text, then execute the following command:
'<,'>!jwt
Which can also be seen in the following Asciicast:
This does not work, however, if you've got the following, where we want to unpack the internal JWT in this JSON object:
{
"client_assertion": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c"
}
For how to do that, check out my article running a command on a visual selection in Vim.