用 Python REPL 会话连接 Vim

Jiv*_*van 7 python vim ipython read-eval-print-loop

我有两个终端会话,一个运行 Vim,另一个运行 Python(或 iPython)REPL。

我正在寻找一种使 Vim 与 REPL 会话动态互操作的方法。

作为所需行为的示例,假设我在 Vim 中打开了这个 Python 文件:

 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))
Run Code Online (Sandbox Code Playgroud)

我在 iPython REPL 会话中输入这些条目:

In [1]: x = 10
In [2]: y = 26
Run Code Online (Sandbox Code Playgroud)

现在我希望能够3-5从 Vim 发送要在 REPL 会话中执行的行,从先前在会话中定义的变量开始,并产生以下结果:

# 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
Run Code Online (Sandbox Code Playgroud)

Emacs 可以很容易地做这样的事情,但尽管搜索了很长时间,我还是没有找到一种方法来获得与 Vim 类似的行为。

编辑:也许答案取决于具体情况(Tmux 等),因此在这种情况下,我专门在 MacOS 上使用两个 iTerm2 窗格,一个运行 Vim,另一个运行 iPython。

Mei*_*ham 6

在 Emacs 世界中,这被称为“Slime”,它通常用于将 Emacs 与 REPL 连接,例如 Lisp REPL。

Vim 中支持 Python 的最接近的东西是vim-slime

此插件要求您使用 GNU Screen 或 Tmux,因此如果您继续在两个单独的终端中运行,则不能指望它可以工作。

另一种选择是使用适用于 Neovim 的 Iron.Nvim。它利用 Neovim 术语支持,因此您不需要 Tmux/Screen。