When I am developing some python script, I regularly find myself running part of the code to see if it does what I want it to do. Currently I do this by copying the code that I want to run to the clipboard, alt-tabbing to the console and then %pasteing the code into ipython.

Of course this procedure is far from ideal, which is why I want to make use of the new :term and term_sendkeys(). I can send code to ipython using term_sendkeys(), but somehow I cannot get ipython to actually run the code. Below is a summary of my steps thus far:

First I start vim 8.1 using vim -u NONE in a normal command prompt on Windows 7. Then I run :term to create the terminal buffer. In this buffer I type <c-w>:echo bufnr('%')<cr> to find the number of the buffer and find that the number is 2. Then I move to the text buffer and run two commands:

:call term_sendkeys(2, "ipython\<cr>")
:call term_sendkeys(2, "a=1\<cr>")

The first starts ipython as expected. The second enters a=1 and a newline. However, when I type the same sequence of keys in ipython hitting enter actual executes the statement instead of starting a new line. I therefore believe that either vim is sending another key than I think it's sending or that ipython treats keys received in this way differently than keys received from the user directly. What am I doing wrong?

  • 1
    Does term_feedkeys work for you? I’m not sure why this is happening though – D. Ben Knoble Jul 10 '19 at 22:19
  • I'm not sure if I understand you correctly. :call term_feedkeys(2, "a=1\<cr>") results in an unknown function error and :h term_feedkeys says there is no help for this function. – Octaviour Jul 11 '19 at 7:56
  • Apologies, you need regular old feedkeys maybe – D. Ben Knoble Jul 11 '19 at 13:05
  • Do you mean something like :buffer 2|feedkeys("a=1\<cr>", 'x')|buffer 1? This produces the same result as term_sendkeys. – Octaviour Jul 11 '19 at 13:19
  • I cannot reproduce your problem with a shell (the example from :h terminal-to-job), regular python, or ipython. For me, all three of those do the correct thing. Note that the help says the job must be in the right state; perhaps your keys are being sent to ipython too early? Yet even for me sending the keys immediately works as expected – D. Ben Knoble Jul 11 '19 at 13:42

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.