How to have a Neovim configuration compatible with Vim
Tags: vim, coding, lua, programming
Published: 2021-12-03 16:23:27

---
title: How to have a Neovim configuration compatible with Vim
published: true
date: 2021-12-01 18:31:15 UTC
tags: vim,coding,lua,programming
canonical_url: https://threkk.medium.com/how-to-have-a-neovim-configuration-compatible-with-vim-b5a46723145e
---

> So you can have your cake and eat it too.

I have been a [Vim](https://vim.org) user for about 8 years already. Most of this time I have written code using [Neovim](https://neovim.io), and when they [announced](https://neovim.io/roadmap/) the Lua integration, I was excited about the possibilities. I use my Vim configuration at home but also at work. In [Booking.com](https://booking.com), we do a reasonable amount of development in remote machines. Typically, you use ssh to remotely access a machine and do your work there. Many of those machines are quite liberal in regards of installing external packages (which includes Neovim), but other are heavily locked. However, we have an internal system that allows you to copy your dot files when you connect to a remote machine.

For most of these years, I have kept my configuration compatible between Vim and Neovim. Every Vim configuration should work on Neovim by default, but the other way around. I managed to keep the especial Neovim features and plug-ins under wards so if needed I could launch the configuration in Vim if Neovim wasn’t available in the environment. This solution will not work anymore when with Lua plug-ins are involved.

### The requirements

These are the **MUST have** for the configuration.

- It must work in Neovim and Vim with similar functionalities so it does not break my muscle memory.
- It must be a single configuration that works out of the box. If not, there will be two different configurations _de facto_.
- Neovim can use Lua and external software (for example, language servers or tools like eslint) without limitations. It will be running in my local machine or in a non critical server.
- Vim can **only** use Vimscript. It can use plug-ins as far as they are written exclusively in Vimscript and can be tracked on git. This will run on restricted servers that only allow to copy configuration files.
- Minimise dependencies but keep it fast (Lua plug-ins > Vim plug-ins > external tools).

### The solution

![Output of the command tree -L 2 -I plugged on $HOME/.config/nvim](https://cdn-images-1.medium.com/max/1024/1*AjmQnC-0nwFXr3Bz6NaqdQ.png)
_How my Neovim configuration directory looks like._

This is how my (Neo)vim configuration looks like. The directory lives in $HOME/.config/nvim, which is the default location for Neovim. When I need to use Vim, I set two symbolic links: `$HOME/.config/nvim` → `$HOME/.vim` and `$HOME/.config/nvim/init.vim` → `$HOME/.vimrc`

- **init.vim** : It is the entry point of the configuration. It loads the plug-ins using vim-plug, which will work for both. It also defined where the plug-ins should be located and which plug-ins to load depending on if it is Neovim or Vim. Additionally, it loads all the other configuration files.
- **common.vim** : It contains the common configuration like some of the bindings, tabs, word wrapping, etc. All these configurations are classical vim configurations and are the same for both. The rule of thumbs is that anything that is valid for both and not a plug-in goes here. If there are inconsistencies between platforms, the Neovim versions takes priority.
- **plugins.vim:** The complementary of **common.vim**. Every plug-in configuration used in both versions goes here for instance, like [NERDTree](https://github.com/preservim/nerdtree), [easymotion](https://github.com/easymotion/vim-easymotion) or [lightline.vim](https://github.com/itchyny/lightline.vim).
- **only\_vim.vim** : Self explanatory. These are the configurations that should be loaded only in Vim. They are mostly inconsistencies between platforms that require extra configuration or Vim only plug-ins. Because it is loaded after, it will overwrite Neovim’s configurations.
- **languages** : This directory contains custom languages configurations used by both versions, with one file per language and one additional file as a kitchen sink. For instance, the file `languages/golang.vim` changes the configuration of the tab to use tabs instead of spaces when editing a `\*.go` file.
- **lua** : Contains all the Lua code for Neovim. It is a requirement of Neovim to place the Lua code here. Neovim configuration takes place and precedence in the common files, which means that this folder mostly contains the configuration of the Lua plug-ins. Every file is the configuration for a concrete plug-in, plus **other.lua** as a kitchen sink. These plug-ins typically require heavy configuration because they set up language servers bindings, syntax highlight, autocomplete, etc. There is an additional folder, languages, which adds additional information required by the plug-ins that can be split by language. Most of the content is the configuration of the different language servers.

![The last lines in the init.vim](https://cdn-images-1.medium.com/max/1024/1*0kzCacM0rIE_KkrzvfKY3w.png)
_The order of loading the plug-ins in init.vim matter_

### Some cool plug-ins that I use and their equivalences

This is a bit off-topic, but this is a selection of some of the plug-ins I use and their equivalences between Vim and Neovim. For the full list of all the plug-ins in my configurations, you can check the init.vimfile [on Github](https://github.com/threkk/dotfiles/blob/master/dotfiles/config/nvim/init.vim). Remember that you can always use the Vim plug-ins in Neovim, but not the other way around.

- [gitsigns.nvim](https://github.com/lewis6991/gitsigns.nvim) → [vim-gitgutter](https://github.com/airblade/vim-gitgutter)
- [telescope.nvim](https://github.com/nvim-telescope/telescope.nvim) → [ctrlp.vim](https://github.com/ctrlpvim/ctrlp.vim)
- [nvim-treesitter](https://github.com/nvim-treesitter/nvim-treesitter) → [vim-polyglot](https://github.com/sheerun/vim-polyglot)
- [indent-blankline](https://github.com/lukas-reineke/indent-blankline.nvim) → [braceless.vim](https://github.com/tweekmonster/braceless.vim)

#### Extra tricks

This snippet will help you to load plug-ins conditionally using vim-plug.

```
" Lazy loading
" From [https://github.com/junegunn/vim-plug/wiki/faq#conditional](https://github.com/junegunn/vim-plug/wiki/faq#conditional-)-
" activation                            
function! Cond(cond, ...)
    let opts = get(a:000, 0, {})     
    return a:cond ? opts : extend(opts, { 'on': [], 'for': [] }) endfunction
```

These two variables can be used for distinguishing between Vim and Neovim. Technically you only need one, but it is more descriptive to have both. Defining the base path also helps as [vim-plug](https://github.com/junegunn/vim-plug) needs to be able to find them in the right place.

```
let is_vim = !has('nvim')
let is_nvim = has('nvim')
  
if is_nvim
    let $BASE = stdpath('config')  
else     
  let $BASE = '$HOME/.vim'                             
endif

call plug#begin($BASE.'/plugged')
```

_Did you like this post? Let me know on [Twitter](https://twitter.com/threkk)!_