18

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

4 Answers 4

23

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
Sign up to request clarification or add additional context in comments.

4 Comments

Hacky. But that's exactly what I want. Thanks.
This seems to work on lines with selection, not on selected text. For example, if a line is "abcde" and I select "bcd" and run for example :'<,'>!python3 -c 'print(input()+"x")' I get "abcdex" instead of "abcdxe". I read somewhere that using `< and `> in place of '< and '> should operate on selection, but doesn't work for me.
It is things like this that make Vim the greatest. Thanks!
@AkiRoss, would you let me know if your problem solved?
1

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

1 Comment

I am aware that ! lets you run a shell command, but I want to copy the selection to a part the shell command. Not just pipe it.
0

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`

2 Comments

Please refrain from downvoting until I can try this tomorrow, mmmkay? :)
How about trying it before answering?
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.