3

I have an SQL file that I have to work on from the command line.

-- Query 1
select 1;
-- Query 2
select *
from metadata;

I've marked line 4 as a (4Gma) and line 5 as b (5Gmb), then execute :'a,'b w !mysql -u root db \| less to view the contents. I've mapped this snippet to F5 so that I can simply reset marks a and b and press F5 to see the result. This works fine.

What I want to do now is insert the results of the query into the document, much like what :r !cmd provides. I know I can replace %s in r !cmd, but %s is replaced with the filename, and I only want to execute the SQL query in the current range.

Is this possible, and if so, how?

2

There is a :h range! which I think will do the trick here.

:'a,'b!mysql -u root db

I tested it with sed on file with content:

line1
line2
line3

Vim commands:

1Gma
3Gmb
:'a,'b!sed -n '/1/p'

Result:

line1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.