Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vim: Run selected code in a persistent REPL-environment

R, Python, Scala etc. all come with REPL-environments, which I don't want to miss, however, most of the time editing text in them sucks, so I edit the code in vim, paste it and look at the output and edit the code in vim again.

I can run the current file with !python % and I can run the current line with even more vim magic, however, this will start a new process of the interpreter.

Is it possible to start a REPL and send lines of code to the running REPL (and get the results back, obviously)?

like image 799
tstenner Avatar asked Oct 26 '11 18:10

tstenner


5 Answers

Maybe one of these two plugins is what you need:

  • slime is screen-based.
  • tslime is a tmux-based version of slime.
like image 93
romainl Avatar answered Nov 19 '22 10:11

romainl


Try Conque:

""" Conque is a Vim plugin which allows you to run interactive programs, such as bash on linux or powershell.exe on Windows, inside a Vim buffer. """

It can easily be configured to open a Python interpreter, and a key mapping can be used to transfer the current line to it to be executed (F9 for the current line, F10 for the current file etc.).

like image 38
Jeet Avatar answered Nov 19 '22 08:11

Jeet


Not for plain-python alone, but if you're using IPython 0.11 or later, take a look at vim-ipython.

Using this plugin, you can send lines or whole files for IPython to execute, and also get back object introspection and word completions in Vim, like what you get with: object?<enter> and object.<tab> in IPython. Additionally, vim-ipython has a "shell" mode, where as you send lines to IPython, you get to see the results those lines produced back in the specialize buffer. See the second screencast on this post

like image 31
Paul Ivanov Avatar answered Nov 19 '22 08:11

Paul Ivanov


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: https://github.com/sillybun/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 <leader>w. And the code will be transmitted to the repl environment.

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

like image 2
Yiteng Zhang Avatar answered Nov 19 '22 10:11

Yiteng Zhang


I recently wrote a plugin for a very similar purpose: vim-notebook which allows the user to keep a background process alive and to make it evaluate part of the current document (and to write the output in the document). It is intended to be used on notebook-style documents containing pieces of code to be evaluated.

like image 2
Thomas Baruchel Avatar answered Nov 19 '22 09:11

Thomas Baruchel