/vimcmdline

Send code to command line interpreter

Primary LanguageVim scriptGNU General Public License v2.0GPL-2.0

vimcmdline: Send lines to interpreter

This plugin sends lines from either Vim or Neovim to a command line interpreter (REPL application). There is support for Clojure, Golang, Haskell, JavaScript, Julia, Jupyter, Kotlin, Lisp, Macaulay2, Matlab, Prolog, Python, Ruby, Sage, Scala, Shell script, and Swift (see Nvim-R for R support on Vim/Neovim). The interpreter runs in either Neovim or Vim built-in terminal. If Tmux is installed, the interpreter can also run in an external terminal emulator or in a tmux pane. The main advantage of running the interpreter in a Neovim terminal is that the output is colorized, as in the screenshot below, where we have different colors for general output, positive and negative numbers, and the prompt line:

nvim running octave

If running in either a Neovim built-in terminal or an external terminal, the plugin runs one instance of the REPL application for each file type. If running in a tmux pane, it runs one REPL application for Vim instance.

How to install

Either use a plugin manager such as Vim-Plug or copy the directories ftplugin, plugin and syntax and their files to your ~/.vim or ~/.config/nvim directory.

You have to install Tmux if you either want to run the interpreter in an external terminal emulator or are using Vim.

I have never adapted the plugin to run the interpreter within Vim's built-in terminal (as it does in Neovim) because Vim cannot colorize the output printed in its terminal.

Usage

If you are editing one of the supported file types, in Normal mode do:

  • <LocalLeader>s to start the interpreter.

  • <Space> to send the current line to the interpreter.

  • <LocalLeader><Space> to send the current line to the interpreter and keep the cursor on the current line.

  • <LocalLeader>q to send the quit command to the interpreter.

For languages that can source chunks of code:

  • In Visual mode, press:

    • <Space> to send a selection of text to the interpreter.
  • And, in Normal mode, press:

    • <LocalLeader>p to send from the line to the end of paragraph.

    • <LocalLeader>b to send block of code between the two closest marks.

    • <LocalLeader>f to send the entire file to the interpreter.

Options

Below are examples of how to set the options in your vimrc:

" vimcmdline mappings
let cmdline_map_start          = '<LocalLeader>s'
let cmdline_map_send           = '<Space>'
let cmdline_map_send_and_stay  = '<LocalLeader><Space>'
let cmdline_map_source_fun     = '<LocalLeader>f'
let cmdline_map_send_paragraph = '<LocalLeader>p'
let cmdline_map_send_block     = '<LocalLeader>b'
let cmdline_map_quit           = '<LocalLeader>q'

" vimcmdline options
let cmdline_vsplit      = 1      " Split the window vertically
let cmdline_esc_term    = 1      " Remap <Esc> to :stopinsert in Neovim's terminal
let cmdline_in_buffer   = 1      " Start the interpreter in a Neovim's terminal
let cmdline_term_height = 15     " Initial height of interpreter window or pane
let cmdline_term_width  = 80     " Initial width of interpreter window or pane
let cmdline_tmp_dir     = '/tmp' " Temporary directory to save files
let cmdline_outhl       = 1      " Syntax highlight the output
let cmdline_auto_scroll = 1      " Keep the cursor at the end of terminal (nvim)

You can also define what application will be run as the interpreter for each supported file type. If you want to do this, create a dictionary called cmdline_app, and add items with the 'filetype' as key and the interpreter as value, as in the example:

let cmdline_app           = {}
let cmdline_app['ruby']   = 'pry'
let cmdline_app['sh']     = 'bash'

If you are using Neovim, you can use its syntax highlight capabilities to colorize the interpreter output, and you can customize the colors in your vimrc in three different ways:

  1. The hex code of the foreground color.

  2. The ANSI number of the foreground color.

  3. The complete highlighting specification.

The example of customization below will work if either your editor supports true colors or if it supports 256 colors (see in Neovim :h tui-colors):

if has('gui_running') || &termguicolors
    let cmdline_color_input    = '#9e9e9e'
    let cmdline_color_normal   = '#00afff'
    let cmdline_color_number   = '#00ffff'
    let cmdline_color_integer  = '#00ffff'
    let cmdline_color_float    = '#00ffff'
    let cmdline_color_complex  = '#00ffff'
    let cmdline_color_negnum   = '#d7afff'
    let cmdline_color_negfloat = '#d7afff'
    let cmdline_color_date     = '#00d7af'
    let cmdline_color_true     = '#5fd787'
    let cmdline_color_false    = '#ff5f5f'
    let cmdline_color_inf      = '#00afff'
    let cmdline_color_constant = '#5fafff'
    let cmdline_color_string   = '#5fd7af'
    let cmdline_color_stderr   = '#0087ff'
    let cmdline_color_error    = '#ff0000'
    let cmdline_color_warn     = '#c0ffff'
    let cmdline_color_index    = '#d7d787'
elseif &t_Co == 256
    let cmdline_color_input    = 247
    let cmdline_color_normal   =  39
    let cmdline_color_number   =  51
    let cmdline_color_integer  =  51
    let cmdline_color_float    =  51
    let cmdline_color_complex  =  51
    let cmdline_color_negnum   = 183
    let cmdline_color_negfloat = 183
    let cmdline_color_date     =  43
    let cmdline_color_true     =  78
    let cmdline_color_false    = 203
    let cmdline_color_inf      =  39
    let cmdline_color_constant =  75
    let cmdline_color_string   =  79
    let cmdline_color_stderr   =  33
    let cmdline_color_error    =  15
    let cmdline_color_warn     =   1
    let cmdline_color_index    = 186
endif

And the next example sets the value of an option as the complete highlighting specification.

let cmdline_color_error = 'ctermfg=1 ctermbg=15 guifg=#c00000 guibg=#ffffff gui=underline term=underline'

If you prefer that the output is highlighted using your current colorscheme, put in your vimrc:

let cmdline_follow_colorscheme = 1

Finally, if you want to run the interpreter in an external terminal emulator, you have to define the command to run it, as in the examples:

let cmdline_external_term_cmd = "gnome-terminal -- /bin/bash -c '%s'"
let cmdline_external_term_cmd = "xterm -e '%s' &"
let cmdline_external_term_cmd = 'kitty %s &'

where %s will be replaced with the terminal command required to run the REPL application in a tmux session. Note that gnome-terminal does not require an & at the end of the command because it forks immediately after startup and that kitty only works if %s is not between quotation marks.

When running in an external terminal emulator, Vimcmdline requires Tmux to work, and generates a tmux.conf for its tmux session. If you rather prefer to write and use your own tmux.conf, you should define its path as in the example:

let cmdline_tmux_conf = "~/vimcmdline_tmux.conf"

Your ~/.inputrc should not include set keymap vi because it would cause some applications to start in vi's edit mode. Then, you would always have to press either a or i in the interpreter console before using it.

How to add support for a new language

  1. Look at the Vim scripts in the ftplugin directory and make a copy of the script supporting the language closer to the language that you want to support.

  2. Save the new script with the name "filetype_cmdline.vim" where "filetype" is the output of echo &filetype when you are editing a script of the language that you want to support.

  3. Edit the new script and change the values of its variables as necessary.

  4. Test your new file-type script by running your application in either Vim or Neovim and using either the built-in terminal or a Tmux split pane.

  5. Look at the Vim scripts in the syntax directory and make a copy of the script supporting the language whose output is closer to the output of the language that you want to support.

  6. Save the new script with the name "cmdlineoutput_app.vim" where "app" is the name of the interpreter. For example, for the "matlab" file-type, the interpreter is "octave".

  7. Edit the new script and change both the pattern used to recognize the input line and the pattern used to recognize errors.

  8. Test your new syntax highlighting script by running your application in a Neovim built-in terminal.

See also

Plugins with similar functionality are neoterm, vim-slime and repl.nvim.