0

I have an external command which accepts a SQL. I want to execute it within vim while editing SQL. I found two ways but both are not very satisfying.

  1. :w !<cmd> This passes the content of the current buffer to <cmd> and outputs the result. However, because the result is shown in vim command line area, if the result is long I cannot scroll up/down the result.

  2. :%! <cmd> This replaces the current buffer content with the cmd output, this is not what I want because I want to preserve the SQL.

If I can see the output in a new horizontal split buffer, it would be very nice. Is there any way to do so?

2 Answers 2

1

Try :terminal <command> % (make sure to save the buffer first).

2
  • Thanks Ben! The terminal window is really satisfying. I wanted to pass the buffer content, not a filename. But this can be achieved by :terminal sh -c "cat % | <cmd>".
    – jyshin
    Mar 8, 2022 at 1:22
  • @jyshin programmatically you could also use term_start() with in_io set to "buffer" and in_buf the buffer number.
    – D. Ben Knoble
    Mar 8, 2022 at 15:01
1
:new +put=system('foobar',0)

Or

Ctrl-Wn"=system("foobar", 0)EnterP

See :h :put, :h system(), :h quote=.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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