2

I am running gnu linux (Linux Mint to be specific). The following is my desired workflow:

  1. I open vim in (say) process 1000 and then start up a python interpreter in process 1001.
  2. I write some code in vim and then select certain lines and then write those lines to the file /proc/1001/fd/0.
  3. At this point I would like the python interpreter to interpret this as code and execute it as if it were typed in directly.

This does not work as desired. Instead the text is displayed on the interpreter's screen, but it is not executed (similar to when error messages of subprocesses are displayed in bash). I presume this has something to do with the fact that my workflow isn't playing well with readline (or some sort of equivalent library). Or my problem might just be that the python interpreter was never designed to be used this way (for presumably security and other reasons).

I understand there are many IDEs with similar functionality, but I was hoping that something simple might work. I'm curious if it's something that can be fixed or if there is something fundamental that I'm misunderstanding.

Raydel Miranda
  • 13,825
  • 3
  • 38
  • 60
Thomas Nyberg
  • 115
  • 1
  • 7
  • What happend if you paste the code directly to the interpreter? – Raydel Miranda Apr 11 '14 at 18:13
  • 1
    For an answer to your literal question, see [this answer](http://stackoverflow.com/a/5380763/27581) or [this answer](http://stackoverflow.com/a/3792108/27581). – Michael Kropat Apr 11 '14 at 18:26
  • Thanks for the info! I see it's more complicated than I thought, but makes sense considering the requirements of a windowing system. – Thomas Nyberg Apr 11 '14 at 18:59
  • Raydel: If I paste the code directly into the interpreter everything is fine. The point seems to be that I misunderstood how the windowing system multiplexes stdin to different windows. It's explained in Michael's links. – Thomas Nyberg Apr 11 '14 at 19:01

1 Answers1

4

It exists and it's called vim-slime

The only requirement is that you run the Python interpreter inside tmux or screen, or even better: byobu

Installing the vim-slime plugin is easy if you're using vim-pathogen:

cd ~/.vim/bundle
git clone git://github.com/jpalardy/vim-slime.git

See the vim-slime page for configuration details, but if you're using tmux, simply add the following to your .vimrc and re-start Vim:

let g:slime_target = "tmux"

Trying it out

Type in some Python code inside Vim:

def fib():
    a, b = 0, 1
    while 1:
        yield a
        a, b = b, a + b

Then press Ctrl-c-Ctrl-c to tell vim-slime to send the contents of your current buffer to another window. The first time you run it, vim-slime will ask you which screen/tmux window to send it to, but after that, press the key-sequence and it will send it wherever you told it to the first time.

vim-slime is visual-mode aware, too! If you only want to send a few lines to Python, enter visual-line mode with V, highlight the lines you want, and press the same Ctrl-c-Ctrl-c key sequence to send just those line.

Michael Kropat
  • 14,557
  • 12
  • 70
  • 91
  • Instead of my (poor) attempt to reinvent the wheel, this seems like the smart way to do it. It works great thanks a lot! – Thomas Nyberg Apr 11 '14 at 18:59