The text editor Vim is available on many Linux systems and is very popular. The editor Neovim is a modern rewrite of Vim, and there are many reasons a user would benefit from sharing a configuration file between them.
In this tutorial, we’ll discuss how to combine their configurations and how to avoid complications that may arise.
2. Using source
First, we’ll look at the method recommended by the documentation for Neovim. It suggests configuring Neovim to use Vim’s configuration file. To do this, we can set init.vim (Neovim’s configuration file) to read Vim’s vimrc. init.vim is located inside ~/.config/nvim/. Let’s edit init.vim to use the ex command source:
If the system’s vimrc is inside the ~/.vim folder, then the path needs to be changed to ~/.vim/vimrc.
2.1. Setting runtimepath and packpath
While Neovim will now read Vim’s configuration file, they both have directories they store various internal information in. Without instructing Neovim to use Vim’s directory, some features may not behave as expected between them. Subsequently, we can remedy this by adding two lines to Neovim’s init.vim:
set runtimepath^=~/.vim runtimepath+=~/.vim/after let &packpath = &runtimepath source ~/.vimrc
These lines are added before the source ~/.vimrc line to configure Neovim to use the correct directories before loading the configuration file. These two lines set the settings runtimepath and packpath, which dictate which directories Neovim looks in for runtime files and package files, respectively. The first line prepends ~/.vim to the setting runtimepath (which is a string) and then appends ~/.vim/after. Also, the second line sets packpath to be identical to runtimepath.
3. Using a Symlink
Using a symlink (also known as soft-link) to make Vim and Neovim share the same configuration file achieves the same results as the source approach. For example, we can use the ln command to link Neovim’s configuration file to Vim’s:
$ ln -s ~/.vimrc ~/.config/nvim/init.vim $ ls -l ~/.vimrc ~/.config/nvim/init.vim -rw-r--r-- 1 kent kent 1072 Feb 28 2021 /home/kent/.vimrc lrwxrwxrwx 1 kent kent 20 Oct 31 2021 /home/kent/.config/nvim/init.vim -> /home/kent/.vimrc
In this way, Vim and Neovim are using the same configuration file. Of course, we should note that changes to the ~/.vimrc file will affect both Vim and Neovim.
4. Resolving Version Discrepancies
Even with Vim and Neovim using the same configuration file, configuration discrepancies still exist. Therefore, we can tell each editor to ignore sections of the configuration file. This is achievable with an if statement. Let’s see the syntax in vimscript to do so:
if has('nvim') " Neovim only features ... endif
has() checks if the currently running version of Vim has a specific feature. Neovim always has the nvim feature, so configuration items inside this statement will only be evaluated by Neovim and not Vim.
There are some features Vim supports that Neovim doesn’t. Therefore, we can write an if statement that does the opposite by prepending ! to the has() check:
if !has('nvim') " Vim only features ... endif
This statement checks for the nvim feature and skips the contents of the if statement on success.
Also, these statements can be combined into a single if-else statement, like so:
if has('nvim') " Neovim only features else " Vim only features endif
4.1. Common Vim-Only or Neovim-Only Settings
Neovim has many new features that Vim doesn’t support. To use these in a shared configuration, they must be in an if statement, as we discussed before. Some common Neovim-only features are:
- Lua builtin
- Native LSP support
- Native Treesitter
The full list is available on Neovim’s help page for nvim-features.
Also, while Neovim attempts to be mostly backward compatible with Vim, there are a handful of features that were incompatible with Neovim, unnecessary, deliberately removed, or missing for another reason. A couple of those common features are:
In this article, we saw that sharing configuration between Vim and Neovim is possible using a symlink or the Ex command source. Also, we learned that we can use the has() function to resolve differences in behaviors between the two text editors.