0

I am working on some Python code, and I would like to call an IPython interpreter from within a script. I have no problem doing this, however, I would then like to dump all of the output from both the script and the interactive session into a new scratch buffer. I know that in general I can use something like

:new | 0read ! <command>

to read the output of a shell command into a new buffer. However, if I try opening up a Python interpreter using that format above what ends up happening is that Vim hangs until I do a bunch of C-c and C-z commands, and finally the output of a Python interpreter with a bunch of C-c and C-z command going into it shows up. Is there any way I could run an interactive program like a Python interpreter, interact with that program as I would normally do, and then have that Python session dumped into a new buffer?

Thanks

8
  • If you can't capture such output outside of Vim (i.e. redirecting to a file from shell) then it's not going to happen in Vim. I think you need to approach this from the Python angle: ask something like "Is there a way to capture REPL session in a file?" at an appropriate SE site. Then Vim can handle it just like any other file. – B Layer Oct 30 '20 at 16:11
  • 2
    OTOH, you should be able to run it in a terminal in Vim...depending on what you are trying to do maybe that's good enough. – B Layer Oct 30 '20 at 16:23
  • @BLayer I have a script that creates a variable of a type I'm unfamiliar with. I need to extract and process information from it. After the script runs, I have an IPython.embed() statement at the end to start up the interactive interpreter so I can play with the variable the script has created. I would like to pull the IPython session back into a scratch buffer so I can use it as a reference and implement what I need from it in the script. I can just use the %history command to save the session to a file and load it that way, I was wondering if there was a native Vim way to do it I suppose. – Dargscisyhp Oct 30 '20 at 16:34
  • r ! and system(), the standard ways of caputring external program output read stdout. That's a well defined and simple interface. Command line interactive programs, OTOH, are not simple and not standardized. For one thing they may be outputting all kinds of ANSI/terminal codes which are apt to make the stdout listener choke. So unless the interactive program has a mode that makes it behave in a well-defined way you can't predict what will happen when you try to slurp it with Vim. Kinda like what you experienced. Run it in a Vim terminal and yank the text you need is probably your best bet. – B Layer Oct 30 '20 at 16:46
  • Welcome to Vi and Vim! – filbranden Oct 30 '20 at 17:07

Your Answer

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

Browse other questions tagged or ask your own question.