I've opened an empty buffer with just vim
.
I have a shell script in the same directory that I can pipe SQL to. That script will print the result of the execution to stdout.
In my empty buffer, I wrote the following.
select * from foo;
I want to pass the entire contents of the file over to my script, exec-sql.sh
, then append the stdout of the shell script back into my buffer.
I tried this first.
% !exec-sql.sh
This replaced the contents of my buffer with the result of the SQL query. Not what I want.
Next, I tried this.
w !exec-sql.sh
This didn't replace the SQL query in by buffer, but it also didn't append the result of the query to my buffer. This also isn't what I want.
I want both the query and the result to end up in my buffer like this.
select * from foo;
id
---
1
2
Is there a way to do this in vanilla Vim?
Update:
I'd also like to just select a range to pass to exec-sql.sh
, maybe by selecting some SQL statements in visual mode and piping just those statements.
:'<,'> !exec-sql.sh
I'd like to keep the selected lines, while also appending the result of executing the SQL commands right below the selected lines.
exec-sql.sh
doesn't read files. You must pipe data to it. In the regular shell I have to do this: cat my.sql | exec-sql.sh
.
I know how to do a regular read from an external command. But I want to do a write first, then a read. Write to the external command's stdin and then read from the external command's stdout.