/nvim-repl

Primary LanguageVim Script

nvim-repl

Create, use, and remove an interactive repl within Neovim.

This plugin uses a Neovim-specific api and is only intended to be used with the latest version of Neovim (0.5.0+). To see if your Neovim is compatible, run:

nvim --version

2 pluggable mappings are provided; they rely on the latest version of Tim Pope's vim-repeat.

🍵 Installation

If using vim-plug, place the following line in the Plugin section of your init.vim / vimrc:

Plug 'tpope/vim-repeat'
Plug 'pappasam/nvim-repl'

Then run the Ex command:

:PlugInstall

I personally use vim-packager, so if you'd like to go down the "package" rabbit hole, I suggest giving that a try.

🧰 Usage

demo

  • :Repl or :ReplOpen
  • without arg: open the default shell which is configured by filetype
  • :Repl env $env_name:open a python shell with the enviorment of $env_name, only support for conda
  • :Repl arg: open the default shell and exec the arg command
  • :ReplClose: close the repl, if open.
  • :ReplToggle: if repl is open, close it. If repl is closed, open it using either the filetype-associated repl or the configured default repl.
  • :ReplClear: clear the repl, if open.
  • :ReplRunCell: will run the cell under the cursor and the cursor will jump to next cell

📖Full Documentation

From within Neovim, type:

:help repl

⌨️Key mappings

Two pluggable mappings are provided. They rely on the latest version of Tim Pope's vim-repeat.

<Plug>ReplSendLine send the current line to the repl. Only mappable in normal mode.

<Plug>ReplSendVisual send the visual selection to the repl. Only mappable in visual mode.

The user should map these pluggable mappings. Example mappings:

nnoremap <leader>rt :ReplToggle<CR>
nnoremap <leader>rc :ReplRunCell<CR>
nmap <leader>rr <Plug>ReplSendLine
vmap <leader>rr <Plug>ReplSendVisual

⚙️Configurations

Use g:repl_filetype_commands to map Neovim filetypes to repls. Eg, if you automatically want to run a "ipython" repl for python filetypes and a "node" repl for JavaScript filetypes, your configuration might look like this:

let g:repl_filetype_commands = {
  \ 'javascript': 'node',
  \ 'python': 'ipython --no-autoindent',
  \ }

⚠️notice: ipython config

  • You should pip install ipython firstly, then let g:repl_filetype_commands = {'python': 'ipython'}

Use g:repl_default to set the default repl if no configured repl is found in g:repl_filetype_commands. Defaults to &shell.

Use g:repl_split to set the repl window position. vertical and horizontal respect the user-configured global splitright and splitbottom settings.

  • 'bottom'
  • 'top'
  • 'left'
  • 'right'
  • 'horizontal'
  • 'vertical' (default)

If split bottom is preferred, then add below line to configuration.

let g:repl_split = 'bottom'

Use g:repl_height to set repl window's height (number of lines) if g:repl_split set 'bottom'/'top'. Default will split equally.

Use g:repl_width to set repl window's width (number of columns) if g:repl_split set 'left'/'right'. Default will vsplit equally.

FAQ

Getting strange errors with Python, please help

One such error might be an IndentError. This has to do with quirks related to the default Python interpreter. To get around this, I suggest using bpython as your default interpreter for Python files. To do this, do the following.

pip install bpython

In your vimrc:

let g:repl_filetype_commands = {
  \ 'python': 'bpython -q',
  \ }

🛩️Written by