2

I've just started wrapping my head around vim+python scripts (having no experience with native vim scripts).

How can I open a new window to contain the stdout from a background process?

Currently, after reading some :help python, the only option I see is something like:

cmd = ":bel new"
vim.command(cmd) 
5
  • Sure you mean window? A window is simply a viewport onto a buffer. Do you want to, say, split the current window into two, and load the stdout of some command into a new buffer in this window? Commented Nov 3, 2010 at 10:43
  • @Sarah: I noticed that too, but wasn't sure how to better phrase it. The latter of what you said makes sense. Commented Nov 3, 2010 at 11:01
  • In that case there's no need to resort to scripting. All you have to do is :new +read\ !ls ... Remember to escape all spaces. :new splits the window, creates an empty buffer and runs read !ls on it. Replace ls with something useful. Commented Nov 3, 2010 at 11:10
  • @Sarah: bua is already writing a Python script for some other reason and needs to do this from within that script. (At least how I understand it.) Commented Nov 3, 2010 at 11:14
  • Fair enough. vim.command("new +read\ !ls") Commented Nov 3, 2010 at 11:16

1 Answer 1

3
+100

Since vim.command can execute most (if not all?) ex commands, you can simply call :new +read!ls from within it.

:new splits the current window and puts a new (empty, no name) buffer into the upper window. It takes an argument +[cmd] which we use to execute read!cmd which reads the stdout of cmd after the bang into the buffer. Be aware that you need to escape spaces in your command with \

All in all you get vim.command("new +read!cmd")

:python vim.command("new +read!ls")

to read in the contents of the current directory into a new buffer in a n cichew, horizontally split window.

If you want to handle escaping of special characters, consider using python's re.escape():

:py import re;vim.command("new +read!"+re.escape("ls Dire*"))

which should be sufficient for most cases. If in doubt, check its documentation and compare it to that of your shell.

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

1 Comment

Doesn't handle escaping, but does answer most of the problem. Doesn't look likely for this question to get more attention, so I'm awarding the bounty.

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.