121

I started using Neovim and moved my .vimrc to .config/nvim/init.vim and other stuff is located in .local/share/nvim.

What is the best way to share configs or even plugins between Vim and Neovim? Symlinks? Can I change the directories in the .vimrc to use only one symlink?

After migrating my vim config to Neovim's default location, this seems to work almost:

ln -s ~/.local/share/nvim/site ~/.vim 
ln -s .config/nvim/init.vim .vimrc

Some plugins (using using vim-plug) don't seem to work correctly. I can't load custom colorschmes nor vim-airline.

1
  • 4
    you can use if has('nvim') for nvim only commands
    – wizzup
    Jul 1, 2017 at 12:04

8 Answers 8

156

TL;DR

You can use your existing ~/.vimrc, files, and plugins located within ~/.vim without having to symlink the files.

And this is now documented in NeoVim manual, see Transitioning from Vim.


For Linux and macOS, just add below lines to the top of your ~/.config/nvim/init.vim, or %LOCALAPPDATA%\nvim\init.vim for Windows.

set runtimepath^=~/.vim runtimepath+=~/.vim/after
let &packpath=&runtimepath
source ~/.vimrc

The above lines will add the existing paths and ~/.vimrc file to your nvim setup.

Credit goes to 👉 vimcast 71 : Meet Neovim

1
  • For anyone finding this and receiving the error because you're using an init.lua file, the lua code is: vim.cmd('set runtimepath^=~/.vim runtimepath+=~/.vim/after') vim.o.packpath = vim.o.runtimepath vim.cmd('source ~/.vimrc') Sep 30 at 1:04
26

You can use if has('nvim'). Here are complete examples:

Configuration only for Neovim:

if has('nvim')
        tnoremap <Esc> <C-\><C-n>
endif

Configuration only for Vim

if !has('nvim')
    set ttymouse=xterm2
endif
18

Having your config in Neovim's default location, do the following:

mkdir -p ~/.local/share/nvim/site
ln -s ~/.local/share/nvim/site ~/.vim 
ln -s .config/nvim/init.vim .vimrc

To fix the colorscheme I had to set to 256 terminal color mode, i.e. add this to your .vimrc (= init.vim) file, which will be ignored by nvim:

set t_Co=256  " Note: Neovim ignores t_Co and other terminal codes.

To get vim-airline I added the following snippet to .vimrc:

set laststatus=2

I don't quite understand why this is necessary only in vim and not in Neovim, but I am not the only one stumbling upon this thing.

6
  • 1
    This has always been necessary in vim. Probably nvim sets this as default and that's why it is working there. Jul 1, 2017 at 19:41
  • 1
    @ChristianBrabandt yes, of course you are right.
    – lumbric
    Jul 3, 2017 at 23:23
  • I reckon commonly people will have ~/.vimrc located in the home folder, as the previous line pointing to ~/.vim.
    – Konrad
    Dec 12, 2017 at 13:09
  • Better to follow the other answer, so that one can have nvim specific config, meanwhile sourcing ~/.vimrc as shared config.
    – ryenus
    Sep 18, 2018 at 3:54
  • 1
    While Neovim ignores t_xx options, I'd argue that it's more correct to wrap something like that with if !has('nvim'), such as in this example. It may ignore them now, but what if that changes in the future? It also implicitly indicates that the option is related to Vim only.
    – ZeroKnight
    Sep 19, 2018 at 7:27
6

Using init.lua

If you prefer to use init.lua instead of init.vim, you can put this code in ~/.config/nvim/init.lua:

vim.cmd([[
set runtimepath^=~/.vim runtimepath+=~/.vim/after
let &packpath = &runtimepath
source ~/.vimrc
]])

Explanation of this code:

you can run VimScript from Lua by running vim.cmd(string). We want to use a multi-line string literal in the passed argument to vim.cmd, so we use Lua's syntax for multi-line string literals, which is opened by [[ and closed by ]]. In that multi-line string line, we run this Vimscript, as recommended by the docs for those transitioning from Vim:

  • Add ~./vim and ~/.vim/after to the list of directories to search for runtime files like filetype.vim, scripts.vim, etc:

    set runtimepath^=~/.vim runtimepath+=~/.vim/after
    
  • Add the full runtime path to the list of directories to use to find Vim packages:

    let &packpath = &runtimepath
    
  • Load ~/.vimrc configuration:

    source ~/.vimrc
    
5

The method here tries to symlink ~/.config/nvim to ~/.vim, and make the conf compatible with vim.

# nvim conf dir: ~/.config/nvim
# vim conf dir: ~/.vim
# link the 1st as the 2nd with relative links

# Prepare a vimrc file in ~/.config/nvim folder
ln -sf ./init.vim ~/.config/nvim/vimrc

# Link the whole ~/.config/nvim folder as ~/.vim foler
ln -sf ./.config/nvim ~/.vim

After this setup, ~/.config/nvim/init.vim is the real file used as conf. ~/.vim/vimrc is just a link to it.

Now we need to make vim reuse nvim's plugin manager, plugins, by changing runtimepath and packpath. In fact, the plugin paths are reused automatically, the following fix is reusing the plugin manger.

let g:is_nvim = has('nvim')
let g:is_vim8 = v:version >= 800 ? 1 : 0

" Reuse nvim's runtimepath and packpath in vim
if !g:is_nvim && g:is_vim8
  set runtimepath-=~/.vim
    \ runtimepath^=~/.local/share/nvim/site runtimepath^=~/.vim 
    \ runtimepath-=~/.vim/after
    \ runtimepath+=~/.local/share/nvim/site/after runtimepath+=~/.vim/after
  let &packpath = &runtimepath
endif

Source: Share confs between vim and nvim

2

Just to add to ipatch answer, which is the correct answer, if you have a slightly different setup from Vim you might need to adapt the path.

For example, on Windows, rather than using the default configuration with:

$HOME
   +--.vimrc
   +--.vim

I am using:

$HOME
   +--vimfiles
          +--vimrc

(The reasons is that is it easier to put a single directory under revision control than a file and a directory, particularly when the file is at the root of your home directory)

The following works for me then:

set runtimepath^=~/vimfiles runtimepath+=~/vimfiles/after
let &packpath=&runtimepath
source ~/vimfiles/vimrc
1

If you want to share ~/.vimrc between vim and neovim without a neovim config file you can define $VIMINIT in your .bashrc or .zshrc file:

export VIMINIT='let $MYVIMRC="$HOME/.vimrc" | source $MYVIMRC'
0

I used the following:

VIMINIT='if has("nvim")|let f="init.lua"|else|let f="vimrc"|end|let $MYVIMRC="~/.config/vim/".f|source $MYVIMRC'

Then put all files in ~/.config/vim, and symlink ~/.config/nvim to it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.