vimteractive: | send commands from text files to interactive programs via vim |
---|---|
Author: | Will Handley |
Version: | 3.0.0 (vimteractive3 branch) |
Homepage: | https://github.com/williamjameshandley/vimteractive |
Documentation: | :help vimteractive |
Vimteractive was inspired by the workflow of the vim-ipython plugin.
This plugin is designed to extend a subset of the functionality of vim-ipython to other REPLs (including ipython). It is based around the unix philosophy of "do one thing and do it well". Vimteractive aims to provide a robust and simple link between text files and language shells. Vimteractive will never aim to do things like autocompletion, leaving that to other, more developed tools such as YouCompleteMe or Copilot.
New in vimteractive3: Dual backend support!
- tmux backend (default): Opens REPLs in external terminal windows via tmux
- Pros: Terminal persists after vim exits, better for long-running sessions
- Cons: Requires tmux, xterm, and xdotool
- vimterminal backend: Uses vim's built-in
:terminal
feature- Pros: No external dependencies, integrated vim experience
- Cons: Terminal closes with vim
Set your backend with let g:vimteractive_backend = 'vimterminal'
or 'tmux'
The activating commands are:
- ipython
:Iipython
- julia
:Ijulia
- maple
:Imaple
- mathematica
:Imathematica
- bash
:Ibash
- zsh
:Izsh
- python
:Ipython
- clojure
:Iclojure
- apl
:Iapl
- R
:IR
- sgpt
:Isgpt
- gpt-command-line
:Igpt
- aichat
:Iaichat
- autodetect based on filetype
:Iterm
Commands may be sent from a text file to the chosen REPL using CTRL-S
.
If there is no REPL, CTRL-S
will automatically open one for you using
:Iterm
.
For supported REPLs (IPython, sgpt, gpt-command-line, aichat, zsh), the output
of the last command may be retrieved with CTRL-Y
.
Note: it's highly recommended to use IPython as your default Python interpreter. You can set it like this:
let g:vimteractive_default_repls = { 'python': 'ipython' }
Important changes in v3:
- Default backend is tmux (external terminals), not vim's native terminal
- New dependency: vim-slime is now required
- Commands changed:
:Ipython
now starts IPython (not:Iipython2
or:Iipython3
)
To get the v2 experience (vim's native terminal):
Add this to your .vimrc
:
" Use vim's built-in terminal instead of tmux
let g:vimteractive_backend = 'vimterminal'
" Optional: Configure terminal split behavior
let g:vimteractive_vimterminal_config = {
\ 'vertical': 1, " Use vertical split
\ 'term_rows': 20 " Set terminal height
\ }
To use the new tmux backend (recommended for long sessions):
Install the dependencies: tmux, xterm (or another terminal), and xdotool. No configuration needed - tmux is the default.
Core Dependencies (both backends):
- Vim 8 or greater
- vim-slime (required for sending text)
Additional Dependencies for tmux backend:
- tmux (for terminal multiplexing)
- xterm or another terminal emulator (configurable via
g:vimteractive_terminal
) - xdotool (for window focus management)
- perl and col (for zsh output processing)
Vimterminal backend requires no additional dependencies!
Installation should be relatively painless via
the usual routes such as
Vundle,
Pathogen or the vim 8 native
package manager (:help packages
).
Important: You must also install vim-slime as it's a required dependency:
" Example for Vundle
Plugin 'jpalardy/vim-slime'
Plugin 'williamjameshandley/vimteractive'
If you're masochistic enough to use
Arch/Manjaro,
you can install vimteractive via the
aur.
For old-school users, there is also a package on the vim
repo.
Depending on your package manager, you may need to run :helptags <path/to/repo/docs>
to install the help documentation.
IPython and Jupyter are excellent tools for exploratory analyses in python. They leverage the interactivity of the python kernel to allow you to keep results of calculations in memory whilst developing further code to process them.
However, I can't stand typing into anything other than vim. Anywhere else, my
screen fills with hjklEB, or worse, I close the window with a careless
<C-w>
. I want a technique that allows me to operate on plain text files,
but still be able to work interactively with the interpreter with minimal
effort.
Many Projects achieve this with a varying level of functionality. Vimteractive aims to create the simplest tool for sending things from text to interpreter, and making it easy to add additional interpreters. In particular, my main aim in starting this was to get a vim-ipython like interface to the command line maple.
To use the key-bindings, you should first disable the CTRL-S
default, which is a terminal command to freeze the output. You can
disable this by putting
stty -ixon
into your .bashrc
(or equivalent shell profile file).
Create a python file test.py
with the following content:
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$')
Now start an ipython interpreter with :Iipython
. An external terminal
window will open running IPython in a tmux session. Position your cursor over
the first line of test.py
, and press CTRL-S
. You should see this line
now appear in the IPython terminal. Do the same with the second and fourth
lines. At the fourth line, you should see a figure appear once it's
constructed with plt.subplots()
. Continue by sending lines to the
interpreter. You can send multiple lines by doing a visual selection and
pressing CTRL-S
.
The REPL runs in an external terminal window attached to a tmux session, allowing you to interact with it directly if needed
You may want to send lines to one REPL from two buffers. To achieve that,
run :Iconn <pane_name>
where <pane_name>
is the name of the tmux pane
containing the REPL (tab completion available). If there is only one REPL,
you can use just :Iconn
.
:Iipython
Activate an ipython REPL:Ijulia
Activate a julia REPL:Imaple
Activate a maple REPL:Imathematica
Activate a mathematica REPL:Ibash
Activate a bash REPL:Izsh
Activate a zsh REPL:Ipython
Activate a python REPL:Iclojure
Activate a clojure REPL:Iapl
Activate an apl REPL:IR
Activate an R REPL:Isgpt
Activate an sgpt REPL:Igpt
Activate an gpt-command-line REPL:Iterm
Activate default REPL for this filetype
CTRL-S
sends lines of text to the interpreter in a mode-dependent manner:
In Normal mode, CTRL-S
sends the line currently occupied by the cursor to the
REPL.
In Insert mode, CTRL-S
sends the line currently being edited, and then
returns to insert mode at the same location.
In Visual mode, CTRL-S
sends the current selection to the REPL.
ALT-S
sends all lines from the start to the current line.
CTRL-Y retrieves the output of the last command sent to the REPL. This is
implemented for the following REPLs: :Iipython
, :Isgpt
, :Igpt
,
:Iaichat
, and :Izsh
In Normal-mode
, CTRL-Y retrieves the output of the last command sent to the
REPL and places it in the current buffer.
In Insert-mode
, CTRL-Y retrieves the output of the last command sent to the
REPL and places it in the current buffer, and then returns to insert mode
after the output.
In Visual-mode
, CTRL-Y deletes the selection and replaces it with the
output of the last command.
:Iconn [{pane_name}]
connects current buffer to REPL in tmux pane
{pane_name}
. You can connect any number of buffers to one REPL.
{pane_name}
can be omitted if there is only one REPL. Tab completion
is available to show all available pane names.
]v
and [v
can be used to cycle between connected buffers in the style of
unimpaired.
If you see strange symbols like ^[[200~
when sending lines to your new
interpreter, you may need to disable bracketed paste for that REPL. The plugin
automatically enables bracketed paste for REPLs in the
g:vimteractive_bracketed_paste
list. To disable it for a specific REPL,
remove it from the list:
" Remove 'python' from bracketed paste list
let g:vimteractive_bracketed_paste = filter(g:vimteractive_bracketed_paste, 'v:val != "python"')
These options can be put in your .vimrc
, or run manually as desired:
" Choose backend: 'tmux' (default) or 'vimterminal'
let g:vimteractive_backend = 'vimterminal' " Use vim's native terminal
" Backend-specific options
let g:vimteractive_terminal = 'xterm -e' " Terminal for tmux backend only
" General options
let g:vimteractive_default_repl = '' " Default REPL (empty = filetype detection)
let g:vimteractive_extract_markdown_code_blocks = 1 " Extract code from markdown responses
let g:vimteractive_zsh_prompt = '^\$' " Regex for zsh prompt detection
let g:vimteractive_zsh_prompt_multiline = 1 " Lines to skip for multiline prompts
This project is very much in an beta phase, so if you have any issues that arise on your system, feel free to leave an issue or create a fork and pull request with your proposed changes
You can easily add your interpreter to Vimteractive, using the following code
in your .vimrc
:
" Mapping from Vimterpreter command to shell command
" This would give you :Iasyncpython command
let g:vimteractive_commands = {
\ 'asyncpython': ['python3', '-m', 'asyncio']
\ }
" The g:vimteractive_bracketed_paste variable is a list of REPLs
" that support bracketed paste mode. Add or remove REPLs as needed:
let g:vimteractive_bracketed_paste += ['asyncpython']
" If you want to set interpreter as default (used by :Iterm),
" map filetype to it. If not set, :Iterm will use interpreter
" named same with filetype.
let g:vimteractive_default_repls = {
\ 'python': 'asyncpython'
\ }
" Note: The g:vimteractive_slow_prompt feature mentioned in some
" documentation is not currently implemented in this branch.