Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pipe visually selected text to a UNIX command and append output to current buffer in Vim

Tags:

sql

vim

vi

editor

Using Vim, I'm trying to pipe text selected in visual mode 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:

<ESC>
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.

like image 382
Damon Snyder Avatar asked Oct 23 '08 15:10

Damon Snyder


1 Answers

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.

like image 173
mweerden Avatar answered Oct 26 '22 08:10

mweerden