My Neovim Setup
Tags: vim, neovim, languageserver, terminal
Published: 2020-07-31 06:02:58

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/wkat7v2z0vdpn7xgrnsx.png)

I’m sure this is done a million times. As my first post, I thought it would be nice to document by Neovim setup or configuration. I spent a lot of time fine-tuning the configuration and I am quite pleased with my efforts. This post covers a top-level general configuration for Neovim, but in most cases it should work for Vim as well.

I have linked the plugins to their respective GitHub repos. Setup instructions are well detailed in the repos’ README file or the GitHub repo’s wiki.

Before I continue to give some details of my configuration, Neovim needs a package manager. For this [vim-plug](https://github.com/junegunn/vim-plug) is easy to use and minimalist in configuration. To install, in a terminal:

    curl -fLo ~/.local/share/nvim/site/autoload/plug.vim --create-dirs \
        https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim

Setup of vim-plug is quite easy. In your .vimrc/init.vim, a simple configuration will include lines:

    call plug#begin('~/.config/nvim/plugged')

    Plug 'ncm2/ncm2'
    Plug '...'

    call plug#end()

The lines in between the call block will include the plugins you need to install. The string within the single-quote is the name of the Github repo and each Plug is a plugin that will be installed. The filepath within the plug#begin will have to match your path to the vim/neovim configuration. The directory name can be any name of your choice. That’s it! You have a package manager ready to use.

I will be covering few aspects to make a robust text editor for your programming needs, these are:

* Code Completion

* Diagnostics & Linting

* Terminal integration

* Git

## **Code Completion**

For code completion, I have been using [neovim-completion-manager-2](https://github.com/ncm2/ncm2) or ncm2 since version 1. Each of the [source providers](https://github.com/ncm2/ncm2/wiki) are separate packages and I would recommend bufword, path, ultisnips (snippet manager), vim at the minimum. For programming languages, you have two options:

1. Install each of the ncm2 sources for the languages you will be using. Each of the sources has instructions to setup. These normally require external packages to be installed that will be needed to integrate with your editor.

1. Install language-server-protocol source ncm2-vim-lsp (in my case) and let it do all the work. Instructions are below.

## Diagnostics & Linting

For diagnostics and linting, we will use the really venerable and text editor agnostic [Language Server Protocol](https://langserver.org/) . There are plenty of blog posts about the benefits of using LSP, primarily it provides an engine for code completion, code diagnostics, reference/definition lookup, formatting and linting. There are plenty of options that are in active development: vim-lsp, LanguageClient-neovim, coc.nvim, ale, vim-lsc. Personally, I have been using [vim-lsp](https://github.com/prabirshrestha/vim-lsp) for my needs. Simply follow the instructions in the repo’s wiki to get setup.

I have setup vim-lsp for Ruby, Python and JavaScript. Each of them require their requisite external packages to be installed for it to work.

* Ruby -> [solargraph](https://github.com/castwide/solargraph)

* Python -> [pyls](https://github.com/palantir/python-language-server)

* JavaScript -> [typescript-language-server](https://github.com/sourcegraph/javascript-typescript-langserver)

Tip: A way to keep your .vimrc/init.vim clean is use the ftplugin directory structure for each of programming filetypes. For instance, all my ruby mappings are stored in the ftplugin/ruby.vim file within my vim root directory. Here is an illustration of my directory structure. Respecting this directory structure will ensure the mappings will get added after your editor has loaded the necessary configurations for that filetype.

    .
    ├── after
    │   └── ftplugin
    ├── autoload
    │   ├── plug.vim
    │   └── plug.vim.old
    ├── coc-settings.json
    ├── ftplugin
    │   ├── javascript.vim
    │   ├── markdown.vim
    │   ├── python.vim
    │   └── ruby.vim
    ├── init.vim

Before you have LSP up and running, ensure you install the ncm2 source for [vim-lsp](https://github.com/ncm2/ncm2-vim-lsp). This will ensure ncm2 will lookup your language server for code completion.

## Terminal Integration

Neovim’s terminal integration is one my favorite features. With the help of a plugin [neoterm](https://github.com/kassio/neoterm), it’s definitely a productivity boost and it has drastically improved my workflow. Creating terminal splits, mapping a command to a shortcut, and the occasional REPLs are few of my frequently used features of the plugin. Here are some handy tips of using a terminal with neoterm/neovim in ex mode:

1. To open a new terminal split :Tnew

1. Toggle open/hide terminal split :1Topen and :1Tclose . The number is the terminal number and it increments for every new terminal you create. Creating every new terminal creates its own vim buffer and allows you to treat them like actual buffers.

1. Sending a command to a particular split :1T bundle exec rails s or :2T bundle exec rails c

1. Map a run command to a shortcut :Tmap ruby foo.rb , this will map the run command to ,tt in normal mode

1. TREPLSendFile and TREPLSendLine will send the respective context to the programming language’s interpreter. pry and irb for Ruby, ipython for Python.

## Git

For Git, I am using Tim Pope’s awesome [vim-fugitive](https://github.com/tpope/vim-fugitive). The commands are well documented. Here are my frequently used commands:

1. :Gstatus and its various sub-commands. This opens a preview window of your modifications, and you can view the various sub-commands by pressing g? in the preview window. cc and ce are my frequently used sub-commands to commit and amend.

1. :Gdiff for your diff needs

1. :Gpush and :Gpull

1. :Gblame for that crucial who dun it view of your files.

## Closing Thoughts

This guide isn’t newbie friendly, so I recommend [vimtutor](https://linux.die.net/man/1/vimtutor) and similar training techniques to get accustomed to vim land. Plugins are a nice addition to vim making it more user friendly at the same time. Some pros will scoff at the idea of using plugins. There is no harm in them as long as it works for you. Neovim or Vim is a matter of personal choice. I chose Neovim, because I feel its leading the development of bringing new features to Vim. Neovim 0.4.0 release brought floating windows, a new lua init support, multi-grids and several other improvements.