Replace vim selection with output of shell command

2019-02-06 03:13发布

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?

4条回答
淡お忘
2楼-- · 2019-02-06 03:30

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`
查看更多
不美不萌又怎样
3楼-- · 2019-02-06 03:34

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

查看更多
beautiful°
4楼-- · 2019-02-06 03:38

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
查看更多
SAY GOODBYE
5楼-- · 2019-02-06 03:47

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.

查看更多
登录 后发表回答