A simple vim package for interfacing with a REPL

An alternative to Nvim-R and vim-slime
Published

May 13, 2025

1 Introduction

Start with youtube Chris T.

https://www.youtube.com/watch?v=lwD8G1P52Sk

Start on zz.tools.vim-R.vim

Here is the code for the plugin:

function! SelectChunk() :execute “normal! ?{\<cr>jV/<cr>k” endfunction

function! MoveNextChunk() :execute “normal! /```{<CR>j” :noh endfunction

function! MovePrevChunk() :execute “normal! 2?```{<CR>j” :noh endfunction

function! Raction(action) :let @c = expand(“”) :let @d=a:action . “(”.@c.”)” :call term_sendkeys(term_list()[0], @d) endfunction

function! SubmitLine() :let @c = getline(“.”) . “” :call term_sendkeys(term_list()[0], @c) endfunction ” function! GetVisualSelection(mode) ” call with visualmode() as the argument let [line_start, column_start] = getpos(“‘<“)[1:2] let [line_end, column_end] = getpos(”’>”)[1:2] let lines = getline(line_start, line_end) if a:mode ==# ‘v’ ” Must trim the end before the start, the beginning will shift left. let lines[-1] = lines[-1][: column_end - (&selection == ‘inclusive’ ? 1 : 2)] let lines[0] = lines[0][column_start - 1:] elseif a:mode ==# ‘V’ else return ’’ endif return join(lines, “”) endfunction

function! Submit() :let y = “source(‘source_visual’,echo=T)” . “” :call term_sendkeys(term_list()[0], y) ” :call delete(‘source_visual’) endfunction

function! Sel() :let @c= GetVisualSelection(visualmode()) . “” :call writefile(getreg(‘c’, 1, 1), “source_visual”) endfunction

function! Brk() :call term_sendkeys(term_list()[0], “<c-c>”) endfunction

function! SubmitEmbed() :let y = “sink(‘temp.txt’); source(‘source_visual’,echo=T); sink()” . “” :call term_sendkeys(term_list()[0], y) ” :call delete(‘source_visual’) endfunction

function! Rd() !sed ‘s/^/# /g’ temp.txt > temp_commented.txt :r !cat temp_commented.txt endfunction

augroup r_rmd_qmd autocmd! autocmd FileType r,rmd,qmd nnoremap :call SubmitLine() autocmd FileType r,rmd,qmd vnoremap :call Sel() |  :call Submit() autocmd FileType r,rmd,qmd nnoremap c  :call Brk() autocmd FileType r,rmd,qmd nnoremap l  :call SelectChunk() | :call Sel() | :call Submit() autocmd FileType r,rmd,qmd nnoremap ;  :call SelectChunk() | :call Sel() | :call Submit() | /```{j autocmd FileType r,rmd,qmd nnoremap k :call MovePrevChunk() autocmd FileType r,rmd,qmd nnoremap j :call MoveNextChunk() autocmd FileType r,rmd,qmd nnoremap r  :vert term R –no-save:wincmd p autocmd FileType r,rmd,qmd nnoremap ZT :!R –quiet -e  ‘render(“%”, output_format=“pdf_document”)’ autocmd FileType r,rmd,qmd nnoremap ZY :!R –quiet -e  ‘quarto_render(“%”, output_format=“pdf”)’ autocmd FileType r,rmd,qmd tnoremap ZD  quarto::quarto_render(output_format = “pdf”) autocmd FileType r,rmd,qmd tnoremap ZO source(“”%“) autocmd FileType r,rmd,qmd tnoremap ZR render(”“%”) autocmd FileType r,rmd,qmd tnoremap ZS style_dir() autocmd FileType r,rmd,qmd tnoremap ZQ q(‘no’)<C->:q! autocmd FileType r,rmd,qmd tnoremap ZZ q(‘no’)<C->:q! autocmd FileType r,rmd,qmd tnoremap lf ls() autocmd FileType r,rmd,qmd nnoremap d :call Raction(“dim”) autocmd FileType r,rmd,qmd nnoremap h :call Raction(“head”) autocmd FileType r,rmd,qmd nnoremap s :call Raction(“str”) autocmd FileType r,rmd,qmd nnoremap p :call Raction(“print”) autocmd FileType r,rmd,qmd nnoremap n :call Raction(“names”) autocmd FileType r,rmd,qmd nnoremap f :call Raction(“length”) autocmd FileType r,rmd,qmd inoremap  A |>0 autocmd FileType r,rmd,qmd vnoremap z  :call Sel() | :call SubmitEmbed() | :call Rd() augroup END

2 Experiment

2.1 Title: Add the normal mode mapping ZY for quarto files.

2.2 Introduction:

The goal is to allow quarto filetypes to render to pdf using a mapping called from the qmd file.

  • start by constructing a mapping in .vimrc: (easier to develop there) map ZY to a shell escape and call to quarto_render. ( use ZT map in rgt-R.vim as a template).

  • test using any index.qmd file in posts. e.g. ~/config_ultisnips/index.qmd.

  • once the mapping works then move it to the plugin and add a autocommand that only adds the mapping for quarto filetype files.

  • open ~/prj/qblog/posts/vim_plugin_zz.tools.vim-R/rgt-R/plugin/rgt-R.vim

  • copy ZT mapping to ZY

  • modify ZY to render quarto files with render_quarto command.

3 Experiment 2

There are two functions in rgt-R that need investigating: 1. SubmitSel and 2. GetVisualSelection

This experiment is to explore and document why SubmitSel sends p copies of the command “source(‘temp.R’) to the R interpreter, where p is the number of lines in the selection.

type(foo) returns code for type of foo. 1 for string, 3 for list

3.1 Prerequisites

In development

3.2 Step-by-Step Implementation

In development

3.3 Key Takeaways

In development

3.4 Further Reading

In development

Reuse

Citation

BibTeX citation:
@online{(ryy)_glenn_thomas2025,
  author = {(Ryy) Glenn Thomas, Ronald},
  title = {A Simple Vim Package for Interfacing with a {REPL}},
  date = {2025-05-13},
  url = {https://focusonr.org/posts/simple_vim_plugin/},
  langid = {en}
}
For attribution, please cite this work as:
(Ryy) Glenn Thomas, Ronald. 2025. “A Simple Vim Package for Interfacing with a REPL.” May 13, 2025. https://focusonr.org/posts/simple_vim_plugin/.