Vimteractive
:vimteractive: 通过 vim 从文本文件向交互式程序发送命令
:Author: Will Handley
:Version: 2.7.1
:Homepage: https://github.com/williamjameshandley/vimteractive
:Documentation: :help vimteractive
Vimteractive 受到
vim-ipython <https://github.com/ivanov/vim-ipython>__ 插件的工作流启发。
该插件旨在将 vim-ipython 的部分功能扩展到其他 REPLs <https://en.wikipedia.org/wiki/Read%E2%80%93eval%E2%80%93print_loop>(包括 ipython)。它基于 unix
的 "do one thing and do it well" <https://en.wikipedia.org/wiki/Unix_philosophy#Do_One_Thing_and_Do_It_Well> 理念。
Vimteractive 旨在在文本文件和语言 shell 之间提供一个健壮且简单的链接。Vimteractive 永远不会试图做诸如
自动补全之类的事情,而是将其留给其他更成熟的工具,例如
YouCompleteMe <https://github.com/Valloric/YouCompleteMe>__ 或
Copilot <https://github.com/features/copilot>__。
激活命令如下:
ipython <https://ipython.readthedocs.io>__:Iipythonjulia <https://julialang.org/>__:Ijuliamaple <https://maplesoft.com/>__:Imaplemathematica <https://www.wolfram.com/mathematica/>__:Imathematicabash <https://en.wikipedia.org/wiki/Bash_(Unix_shell)>__:Ibashzsh <https://www.zsh.org/>__:Izshpython <https://www.python.org/>__:Ipythonclojure <https://clojure.org/>__:Iclojureapl <https://en.wikipedia.org/wiki/APL_(programming_language)>__:IaplR <https://www.r-project.org/>__:IRsgpt <https://github.com/TheR1D/shell_gpt>__:Isgptgpt-command-line <https://github.com/kharvd/gpt-cli>__:Igpt- 基于文件类型自动检测
:Iterm
可以使用 CTRL-S 从文本文件向选定的 REPL 发送命令。
如果没有 REPL,CTRL-S 会使用
:Iterm 自动为您打开一个。
对于某些终端,可以使用
CTRL-Y 获取最后一条命令的输出。
注意:强烈建议使用 IPython 作为默认的 Python 解释器。您可以这样设置:
.. code:: vim
let g:vimteractive_default_shells = { 'python': 'ipython' }
安装
由于此包利用了 vim 原生交互式终端,vimteractive 仅兼容 vim 8 或更高版本。
通过
the usual routes <https://vimawesome.com/plugin/vimteractive>_ 等
Vundle <https://github.com/VundleVim/Vundle.vim>、
Pathogen <https://github.com/tpope/vim-pathogen> 或 vim 8 原生
包管理器(:help packages)进行安装应该相对轻松。
如果你足够受虐,使用
Arch <https://wiki.archlinux.org/index.php/Arch_Linux>/Manjaro <https://manjaro.org/>,
你可以通过
aur <https://aur.archlinux.org/packages/vim-vimteractive>__ 安装 vimteractive。
对于老派用户,vim 仓库 <https://www.vim.org/scripts/script.php?script_id=5687>__ 上也有一个包。
根据你的包管理器,你可能需要运行 :helptags <path/to/repo/docs> 来安装帮助文档。
动机
IPython and Jupyter <https://ipython.org/>__ 是用于
python 探索性分析的绝佳工具。它们利用 python
内核的交互性,允许你在开发进一步处理它们的代码时,
将计算结果保留在内存中。
然而,我受不了在 vim 以外的任何地方打字。在其他任何地方,我的
屏幕都会充满 hjklEB,或者更糟,我会不小心用
<C-w> 关闭窗口。我想要一种技术,让我能够操作纯文本文件,
但仍然能够以最小的
努力与解释器进行交互式工作。
Many Projects <#similar-projects>__ 以不同水平的
功能实现了这一点。Vimteractive 旨在创建最简单的工具,用于将内容
从文本发送到解释器,并轻松添加额外的解释器。特别是,
我启动这个项目的主要目标是获得一个类似 vim-ipython 的
命令行 maple <https://www.maplesoft.com/>__ 界面。
用法
要使用按键绑定,您应首先禁用 CTRL-S
默认设置,该设置是一个用于冻结输出的终端命令。您可以通过放置
.. code:: bash
stty -ixon
到你的 .bashrc(或等效的 shell 配置文件)中。
用法示例:
|example_usage|
创建一个 Python 文件 ``test.py``,内容如下:
.. code:: python
import matplotlib.pyplot as plt
import numpy
fig, ax = plt.subplots()
x = numpy.linspace(-2,2,1000)
y = x**3-x
ax.plot(x, y)
ax.set_xlabel('$x$')
ax.set_ylabel('$y$')
现在在 vim 中启动一个 ipython 解释器,使用 ``:Iipython``。你应该看到上方打开了一个预览窗口,其中显示你的 ipython 提示符。将光标定位在 ``test.py``, and press ``CTRL-S`` 的第一行上。你应该看到这一行现在出现在预览窗口的第一个提示符中。对第二行和第四行执行相同操作。在第四行时,一旦使用 ``plt.subplots()`` 构建完成,你应该会看到一个图形出现。继续向解释器发送行。你可以通过进行可视选择并按下 ``CTRL-S`` 来发送多行。
如果你使用 ``CTRL-W+k`` 切换窗口,你会看到终端缓冲区切换为更常见的普通模式缓冲区,从中你可以执行传统的普通模式命令。但是,如果你尝试插入,你将进入 REPL,并能够像在命令行中运行一样,交互式地向提示符输入命令。如果其中包含有价值的输出,你可以将此缓冲区保存到一个新文件中。
你可能希望从两个缓冲区向同一个 REPL 发送行。为了实现这一点,运行 ``:Iconn <buffer_name>`` where ``<buffer_name>``,这是包含 REPL 的缓冲区名称。如果只有一个 REPL,你可以仅使用 ``:Iconn``。
支持的 REPL
:IipythonActivate an ipython REPL:IjuliaActivate a julia REPL:ImapleActivate a maple REPL:ImathematicaActivate a mathematica REPL:IbashActivate a bash REPL:IzshActivate a zsh REPL:IpythonActivate a python REPL:IclojureActivate a clojure REPL:IaplActivate an apl REPL:IRActivate an R REPL:IsgptActivate an sgpt REPL:IgptActivate an gpt-command-line REPL:ItermActivate default REPL for this filetype
Sending commands
``CTRL-S`` 以依赖模式的方式向解释器发送文本行:
在 Normal 模式下,``CTRL-S`` 将光标当前所在的行发送到
REPL。
在 Insert 模式下,``CTRL-S`` 发送当前正在编辑的行,然后
返回到同一位置的插入模式。
在 Visual 模式下,``CTRL-S`` 将当前选区发送到 REPL。
``ALT-S`` 发送从开头到当前行的所有行。
获取命令输出
CTRL-Y 获取发送到 REPL 的上一条命令的输出。这仅在
部分终端中实现(:Iipython, :Isgpt and :Igpt)
在 Normal-mode 中,CTRL-Y 获取发送到 REPL 的上一条命令的输出
并将其放入当前缓冲区。
在 Insert-mode 中,CTRL-Y 获取发送到 REPL 的上一条命令的输出
并将其放入当前缓冲区,然后在输出之后返回插入模式。
连接到现有的 REPL
``:Iconn [{buffer]`` connects current buffer to REPL in ``{buffer}``。您可以
将任意数量的缓冲区连接到一个 REPL。如果只有一个 REPL,则可以省略 ``{buffer}``。
``]v`` and ``[v`` 可用于以
`unimpaired <https://github.com/tpope/vim-unimpaired>`__ 的风格在已连接的缓冲区之间循环切换。
常见问题
-------------
括号粘贴
~~~~~~~~~~~~~~~
如果您在向新的
解释器发送行时看到诸如 ``^[[200~`` 之类的奇怪符号,您可能处于一个未启用括号粘贴的旧系统上,
或者存在其他 shell 行为问题。您可以通过以下方式更改默认
设置
.. code:: vim
let g:vimteractive_bracketed_paste_default = 0
选项
-------
这些选项可以放在你的 ``.vimrc`` 中,或根据需要手动运行:
.. code:: vim
let g:vimteractive_vertical = 1 " 垂直分割 REPL
let g:vimteractive_autostart = 0 " 默认不启动 REPL
扩展功能
-----------------------
本项目目前仍处于 beta 阶段,因此如果您在系统中遇到任何问题,
请随时 `leave an issue <https://github.com/williamjameshandley/vimteractive/issues/new>`__ 或创建一个 `fork and pull
request <https://gist.github.com/Chaser324/ce0505fbed06b947d962>`__,
附上您建议的更改
您可以使用以下代码轻松将您的解释器添加到 Vimteractive,
在您的 ``.vimrc`` 中:
.. code:: vim
" 从 Vimterpreter 命令到 shell 命令的映射
" 这将为您提供 :Iasyncpython 命令
let g:vimteractive_commands = {
\ 'asyncpython': 'python3 -m asyncio'
\ }
" 如果您在将行发送到新解释器时看到诸如 ^[[200~ 之类的奇怪符号,
" 请为其禁用 bracketed paste。
" 如果您的 shell 以某种方式表现异常,您也可以尝试此操作。
" 任何标准 Python REPL 都需要此功能,包括
" python3 -m asyncio
let g:vimteractive_bracketed_paste = {
\ 'asyncpython': 0
\ }
" 如果您想将解释器设置为默认值(由 :Iterm 使用),
" 请将 filetype 映射到它。如果未设置,:Iterm 将使用
" 与 filetype 同名的解释器。
let g:vimteractive_default_shells = {
\ 'python': 'asyncpython'
\ }
" 如果您的解释器启动时间较长,您可能希望在发送命令前
" 等待一段时间。在此字典中设置毫秒数以实现此功能。
" 这对于 python3 不是必需的,但对于其他 REPL(如 Clojure)
" 可能很有用。
let g:vimteractive_slow_prompt = {
\ 'asyncpython': 200
\ }
Similar projects
----------------
- `vim-ipython <https://github.com/ivanov/vim-ipython>`__
- `vim-notebook <https://github.com/baruchel/vim-notebook>`__
- `conque <https://code.google.com/archive/p/conque/>`__
- `vim-slime <https://github.com/jpalardy/vim-slime>`__
- `tslime_ipython <https://github.com/eldridgejm/tslime_ipython>`__
- `vipy <https://github.com/johndgiese/vipy>`__
.. |example_usage| image:: https://raw.githubusercontent.com/williamjameshandley/vimteractive/master/images/example_usage.gif