Jivan
Jivan

Reputation: 23068

Connect Vim with Python REPL session

I have two terminal sessions, one running Vim and the other running a Python (or iPython) REPL.

I'm looking for a way to make Vim dynamically interoperate with the REPL session.

As an example of desired behaviour, say I have this Python file open in Vim:

 1  x = 40
 2  y = 2
 3  z = x + y
 4  print('The answer is {}'.format(z))
 5  print('The product of {} and {} is {}'.format(x, y, x*y))

And I type these entries in the iPython REPL session:

In [1]: x = 10
In [2]: y = 26

Now I'd like to be able to send lines 3-5 from Vim to be executed in the REPL session, starting with the variables previously defined in the session, and producing the following result:

# these are lines typed in the REPL
In [1]: x = 10
In [2]: y = 26

# lines from Vim are silently inserted here and executed, which prints...
The answer is 36
The product of 10 and 26 is 260

# because of Vim export, z is now part of the current scope
In [3]: z
Out[3]: 36

Emacs can do such stuff quite easily, but despite searching for quite a long time I never found a way to get similar behaviour with Vim.

Edit: Maybe the answer would depend on the specifics (Tmux, etc) so in this case, I'm specifically using two iTerm2 panes on MacOS, one running Vim and the other running iPython.

Upvotes: 12

Views: 7825

Answers (4)

Daan
Daan

Reputation: 283

I recommend the vimcmdline Vim plugin.

This plugin sends lines from either Vim or Neovim to a command line interpreter (REPL application). There is support for Clojure, Golang, Haskell, JavaScript, Julia, Jupyter, Kotlin, Lisp, Macaulay2, Matlab, Prolog, Python, Ruby, Sage, Scala, Shell script, Swift and TypeScript (see Nvim-R for R support on Vim/Neovim). The interpreter runs in Neovim's built-in terminal. If Tmux is installed, the interpreter can also run in an external terminal emulator or in a tmux pane.

(https://github.com/jalvesaq/vimcmdline)

vimcmdline adds shortcuts to Vim to open a REPL and send bits of text from the Vim buffer to the REPL for evaluation, for instance the current line, selected text or the function definition under the cursor.

tmux adds a lot of flexibility to where your code is sent. You can move the tmux pane with the REPL that vimcmdline has opened to another tmux session and attach to that session in a different terminal, possibly on a different monitor. You can also nest a tmux session within the tmux pane to which vimcmdline is sending code and open several REPL's in different panes or windows to have separate environments in which to evaluate code, e.g. one normal REPL and one for interactive debugging. I do these things with a non-gui Neovim running within tmux.

I don't think vimcmdline cares too much about which REPL you are using or what programming language, except for special features such sending function definitions. You could thus use it with pretty much any interpreted programming language.

Upvotes: 1

eric
eric

Reputation: 41

Maybe you can try my plugin vim-repl. It provides a convince repl environment for vim using the vim8 terminal feature.

here is the github homepage: vim-repl

To open the repl environment, just run :REPLToggle or you can even bind it witk key like:

nnoremap <leader>r :REPLToggle<Cr>

To interact with repl, you just select the code and press ww. And the code will be transmitted to the repl environment.

Look into the github homepage for more details, it will worth your time.

Upvotes: 4

Eric Wong
Eric Wong

Reputation: 265

I wrote a REPL Framework for vim/neovim, and will push it to:

https://github.com/SpaceVim/SpaceVim/pull/1110

in this PR, I will use neovim/vim 's job feature to start a REPL process, and community with the process via channel. all the result will be shown in a split buffer.

Upvotes: 0

Meitham
Meitham

Reputation: 9670

In Emacs world, this is referred to as "Slime", and it usually used to connect Emacs with a REPL, such as Lisp REPL.

The closest thing in Vim, that supports Python is vim-slime

This plugin requires you to use either GNU Screen or Tmux, so you cannot expect it to work if you continue running in two separate terminals.

Another option is to use Iron.Nvim which works on Neovim only. It makes use of Neovim term support so you don't need Tmux/Screen.

Upvotes: 6

Related Questions