Replace vim selection with output of shell command

我的未来我决定 提交于 2019-12-18 12:17:12

问题


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.



来源:https://stackoverflow.com/questions/6932382/replace-vim-selection-with-output-of-shell-command

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!