w3hello.com logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML videos Categories
Linux: write to stdin of python interpreter process and have that process evaluate input as code

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.

© Copyright 2018 w3hello.com Publishing Limited. All rights reserved.