Skip to content

Instantly share code, notes, and snippets.

@b-d-ln
Forked from rohitfarmer/nvimr-demo.md
Created August 3, 2021 11:24
Show Gist options
  • Select an option

  • Save b-d-ln/1d49a469a1b0fc5c7a4bd7e7daa516d2 to your computer and use it in GitHub Desktop.

Select an option

Save b-d-ln/1d49a469a1b0fc5c7a4bd7e7daa516d2 to your computer and use it in GitHub Desktop.
Nvim-R Demo

How to use Neovim or VIM as an IDE for R

Note: This tutorial is written for Linux based systems.

Requirements

R >= 3.0.0

To install the latest version of R please flollow the download and install instructions at https://cloud.r-project.org/

Neovim >= 0.2.0

Neovim (nvim) is the continuation and extension of Vim editor with the aim to keep the good parts of Vim and add more features. In this tutorial I will be using Neovim (nvim), however, most of the steps are equally applicable to Vim also. Please follow download and installation instructions on nvim's GitHub wiki https://github.com/neovim/neovim/wiki/Installing-Neovim.

OR

Vim >= 8.1

Vim usually comes installed in most of the Linux based operating system. However, it may not be the latest one. Therefore, to install the latest version please download and install it from Vim's GitHub repository as mentioned below or a method that is more confortable to you.

git clone https://github.com/vim/vim.git
make -C vim/
sudo make install -C vim/

Plugin Manager

There are more than one plugin manager's available for Vim that can be used to install the required plugins. In this tutorial I will be using vim-plug pluggin manager.

Plugins

In the end below are the plugins that we would need to convert Vim editor into a fully functional IDE for R.

  1. Nvim-R: https://github.com/jalvesaq/Nvim-R
    • Nvim-R is the main plugin that will add the functionality to execute R code from within the Vim editor.
  2. Ncm-R: https://github.com/gaalcaras/ncm-R
    • Ncm-R adds synchronous auto completion features for R.
    • It is based on ncm2 and nvim-yarp plugins.
  3. Nerd Tree: https://github.com/preservim/nerdtree
    • Nerd Tree will be used to toggle file explorer in the side panel.
  4. Lightline.vim: https://github.com/itchyny/lightline.vim
    • Lineline.vim adds asthetic enhancements to Vim's statusline/tabline.

Procedure

  1. Make sure that you have R >=3.0.0 installed.
  2. Make sure that you have Neovim >= 0.2.0 installed.
  3. Install the vim-plug plugin manager.
curl -fLo ~/.local/share/nvim/site/autoload/plug.vim --create-dirs \
    https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim
  1. Install the required plugins.

First, create an init.vim file in ~/.config/nvim folder (create the folder if it doesn't exist). This file is equivalent to a .vimrc file in the traditional Vim environment. To init.vim file start adding:

" Specify a directory for plugins
" - Avoid using standard Vim directory names like 'plugin'
call plug#begin('~/.vim/plugged')

" List of plugins.
" Make sure you use single quotes

" Shorthand notation
Plug 'jalvesaq/Nvim-R'
Plug 'ncm2/ncm2'
Plug 'roxma/nvim-yarp'
Plug 'gaalcaras/ncm-R'
Plug 'preservim/nerdtree'
Plug 'itchyny/lightline.vim'

" Initialize plugin system
call plug#end()
  1. Update and add more features to the init.vim file.
" Set a Local Leader

" With a map leader it's possible to do extra key combinations
" like <leader>w saves the current file
let mapleader = ","
let g:mapleader = ","


" Plugin Related Settings

" NCM2
autocmd BufEnter * call ncm2#enable_for_buffer()    " To enable ncm2 for all buffers.
set completeopt=noinsert,menuone,noselect           " :help Ncm2PopupOpen for more
                                                    " information.

" NERD Tree
map <leader>nn :NERDTreeToggle<CR>                  " Toggle NERD tree.

" LightLine.vim 
set laststatus=2                " To tell Vim we want to see the statusline.


" General NVIM/VIM Settings

" Tabs & Navigation
map <leader>tnew :tabnew<cr>    " To create a new tab.
map <leader>to :tabonly<cr>     " To close all other tabs (show only the current tab).
map <leader>tc :tabclose<cr>    " To close the current tab.
map <leader>tm :tabmove<cr>     " To move the current tab to next position.
map <leader>tn :tabn<cr>        " To swtich to next tab.
map <leader>tp :tabp<cr>        " To switch to previous tab.


" Line Numbers & Indentation
set backspace=indent,eol,start  " To make backscape work in all conditions.
set ma                          " To set mark a at current cursor location.
set number                      " To switch the line numbers on.
set expandtab                   " To enter spaces when tab is pressed.
set smarttab                    " To use smart tabs.
set autoindent                  " To copy indentation from current line 
                                " when starting a new line.
set si                          " To switch on smart indentation.


" Search
set ignorecase                  " To ignore case when searching.
set smartcase                   " When searching try to be smart about cases.
set hlsearch                    " To highlight search results.
set incsearch                   " To make search act like search in modern browsers.
set magic                       " For regular expressions turn magic on.


" Brackets
set showmatch                   " To show matching brackets when text indicator 
                                " is over them.
set mat=2                       " How many tenths of a second to blink 
                                " when matching brackets.


" Errors
set noerrorbells                " No annoying sound on errors.


" Color & Fonts
syntax enable                   " Enable syntax highlighting.


" Return to last edit position when opening files
au BufReadPost * if line("'\"") > 1 && line("'\"") <= line("$") | exe "normal! g'\"" | endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment