Skip to content

Custom Key Bindings

James Eapen edited this page Jan 25, 2024 · 14 revisions

When creating custom key bindings for Nvim-R, it is necessary to create three maps for most functions because the way the function is called is different in each Vim mode. Thus, key bindings must be made for Normal mode, Insert mode, and Visual mode.

To customize a key binding you could put something like this in your vimrc:

function! s:customNvimRMappings()
   nmap <buffer> <Leader>sr <Plug>RStart
   imap <buffer> <Leader>sr <Plug>RStart
   vmap <buffer> <Leader>sr <Plug>RStart
endfunction
augroup myNvimR
   au!
   autocmd filetype r call s:customNvimRMappings()
augroup end

The above example shows how to add a key binding to start R for sr for just r buffers. The default rf still exists. After changing the maps in your vimrc, you have to restart Vim. If you want to remove the default mappings, you can set R_user_maps_only.

Only the custom key bindings for Normal mode are shown in Vim's menu, but you can type :map to see the complete list of current mappings, and below is the list of the names for custom key bindings (the prefix RE means "echo"; RD, "cursor down"; RED, both "echo" and "down"):

Star/Close R

  • RStart
  • RCustomStart
  • RClose
  • RSaveClose

Clear R console

  • RClearAll
  • RClearConsole

Edit R code

  • RIndent
  • RNextRChunk
  • RPreviousRChunk

Send line or part of it to R

  • RSendLine
  • RDSendLine
  • RSendLAndOpenNewOne
  • RNLeftPart
  • RNRightPart
  • RILeftPart
  • RIRightPart
  • RDSendLineAndInsertOutput

Send code to R console

  • RSendAboveLines
  • RSendSelection
  • RESendSelection
  • RDSendSelection
  • REDSendSelection
  • RSendMotion
  • RSendSelAndInsertOutput
  • RSendMBlock
  • RESendMBlock
  • RDSendMBlock
  • REDSendMBlock
  • RSendParagraph
  • RESendParagraph
  • RDSendParagraph
  • REDSendParagraph
  • RSendFunction
  • RESendFunction
  • RDSendFunction
  • REDSendFunction
  • RSendFile

Send command to R

  • RHelp
  • RPlot
  • RSPlot
  • RShowArgs
  • RShowEx
  • RShowRout
  • RObjectNames
  • RObjectPr
  • RObjectStr
  • RViewDF (view data.frame or other object)
  • RViewDFs (view object in split window)
  • RViewDFv (view object in vertically split window)
  • RViewDFa (view object in split window above the current one)
  • RDputObj
  • RSetwd
  • RSummary
  • RListSpace

Support to Sweave and knitr

  • RSendChunk
  • RDSendChunk
  • RESendChunk
  • REDSendChunk
  • RSendChunkFH (from the first chunk to here)
  • RBibTeX (Sweave)
  • RBibTeXK (Knitr)
  • RSweave
  • RKnit
  • RMakeHTML
  • RMakePDF (Sweave)
  • RMakePDFK (Knitr)
  • RMakePDFKb (.Rmd, beamer)
  • RMakeODT (.Rmd, Open document)
  • RMakeWord (.Rmd, Word document)
  • RMakeRmd (rmarkdown default)
  • RMakeAll (rmarkdown all in YAML)
  • ROpenPDF
  • RSyncFor (SyncTeX search forward)
  • RGoToTeX (Got to LaTeX output)
  • RSpinFile
  • RNextRChunk
  • RPreviousRChunk

Support to Quarto

  • RQuartoRender
  • RQuartoPreview
  • RQuartoStop

Open attachment of bib item

  • ROpenRefFile

Debugging functions

  • RDebug
  • RUndebug

Object browser

  • RUpdateObjBrowser
  • ROpenLists
  • RCloseLists

See also: R_user_maps_only and R_disable_cmds.

The completion of function arguments only happens in Insert mode.

The plugin also contains a function called RAction which allows you to build ad-hoc commands to R. This function takes the name of an R function such as levels or table and the word under the cursor, and passes them to R as a command.

For example, if your cursor is sitting on top of the object called gender and you call the RAction function, with an argument such as levels, Vim will pass the command levels(gender) to R, which will show you the levels of the object gender. To make it even easier to use this and other functions, you could write custom key bindings in your vimrc, as in the examples below:

   nmap <silent> <LocalLeader>rk :call RAction("levels")<CR>
   nmap <silent> <LocalLeader>t :call RAction("tail")<CR>
   nmap <silent> <LocalLeader>H :call RAction("head")<CR>

If you want an action over an selection, then the second argument must be the string "v":

   vmap <silent> <LocalLeader>h :call RAction("head", "v")<CR>

In this case, the beginning and the end of the selection must be in the same line.

If either a second or a third optional argument starts with a comma, it will be inserted as argument(s) to the RAction function. Examples:

   nmap <silent> <LocalLeader>h :call RAction('head', ', n = 10')<CR>
   nmap <silent> <LocalLeader>t :call RAction('tail', ', n = 10, addrownums = FALSE')<CR>

If the command that you want to send does not require an R object as argument, you can create a shortcut to it by following the example:

   map <silent> <LocalLeader>s :call g:SendCmdToR("search()")<CR>

See also: R_source