Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace vim selection with output of shell command

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?

like image 508
harithski Avatar asked Aug 03 '11 19:08

harithski


People also ask

How do you get out of replace in Vim?

Press y to replace the match or l to replace the match and quit. Press n to skip the match and q or Esc to quit substitution. The a option substitutes the match and all remaining occurrences of the match.

How do I repeat and replace in Vim?

Change and repeat Search for text using / or for a word using * . In normal mode, type cgn (change the next search hit) then immediately type the replacement. Press Esc to finish. From normal mode, search for the next occurrence that you want to replace ( n ) and press . to repeat the last change.

How do I change the command in Vim?

To quickly change a word you can use cw , caw or ciw . Use c$ or just C to quickly change from the cursor to the end of a line, cc to change an entire line, or cis for a sentence. The standard change word command requires you to type cw , then a new word, then press Escape.

Which command is used to replace a character in vi editor?

Position the cursor over the character and type r , followed by just one replacement character. After the substitution, vi automatically returns to command mode (you do not need to press Esc).


2 Answers

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
like image 50
Michał Trybus Avatar answered Sep 21 '22 12:09

Michał Trybus


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

like image 28
Jonatan Avatar answered Sep 22 '22 12:09

Jonatan