tags:

views:

1335

answers:

2

Using Vim, I'm trying to pipe visually selected text to a UNIX command and have the output appended to the end of the current file. For example, say we have a SQL command such as:

SELECT * FROM mytable;

I want to do something like the following:

V # select text :'<,'>!mysql -uuser -ppass mydb

But instead of having the output overwrite the currently selected text, I would like to have the output appended to the end of the file. You probably see where this is going. I'm working on using VIM as a simple SQL editor. That way, I don't have to leave VIM to edit, tweak, test SQL code.

Thanks!

+5  A: 

How about copying the selected text to the end of the file, select the copy and run the command? If you do not want to repeat the same commands over and over again, you can record the sequence by using q or add a new command. I have tried the latter as follows:

:com -range C <line1>,<line2>yank | $ | put | .,$ !rev

With it you can select some lines and then type :C. This will first yank the selection, then go to the end of the file, paste the yanked text and run the command (rev in this case) over the new text.

mweerden
A: 

This answer is really good but what if I just want to pipe the selected text to a shell command and receive the one-line output from this shell command on the vim info/command line?

What I'm really trying to do: Pipe the selected text to a pastebin-type shell command and I want to receive the output of the shell cmd (which is the http link to the pastebin). Is this possible?

charding