Replace vim selection with output of shell command

2019-02-06 03:08发布

问题:

I need to pass some selected text in vim to a curl command as a parameter value. For example. I need to be able to run

curl -sSd css="body { border-radius: 5px; }" http://prefixr.com/api/index.php

from vim. Obviously, the "body { border-radius: 5px; }" part will be dynamic. Usually, a visual mode selection in vim.

How do I get the selected text and pass it as a parameter to curl?

回答1:

You can use the :! command to filter selected text through an external program. The text is fed to stdin and substituted with the results from stdout.

In this case you'll have to use cat and command substitution to feed the lines as a parameter to curl, like so:

:'<,'>!curl -sSd css="`cat`" http://prefixr.com/api/index.php


回答2:

By selecting one or more rows and using :! you can pass these lines to a command, for example:

So sort an entire file using the sort command, try this: ggVG !sort, which should look like this in your editor:

B

C

A

:'<,'>!sort



回答3:

Can't test this right now so not 100% sure if it will work

esc, followed by

:r ! curl -sSd="`cat`" http://prefixr.com/api/index.php`


回答4:

For piping words without gratuitous newlines, see this example to uppercase selected text:

select-region c Control-r = system("perl -pe '$=uc($)'", @")

Explanation: select region, c is to (change selection), C-r to execute expression. Note: dollar is dollar underscore, but underscore not visible after posting.