7

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.

1
  • Thanks! That worked as expected. Here is the actual command I used: com -range C <line1>,<line2>yank | $ | put | .,$ !/usr/local/mysql/bin/mysql -uuser mydb I had a "select * from table;" line in the text file and I just <ESC>:C and it dumped the output at the end of the file. Commented Oct 23, 2008 at 17:42

3 Answers 3

7

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.

Sign up to request clarification or add additional context in comments.

Comments

1

If you prefer more programmatic approach, you can have

:call append(line("$"), system("command", GetSelectedText()))

where GetSelectedText is the reusable function:

func! GetSelectedText()
  normal gv"xy
  let result = getreg("x")
  normal gv
  return result
endfunc

Comments

0

Try

:r | YourCommand

For example:

:r ! echo foo

adds foo to your buffer.

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.