Make Vim And Neovim Work With Differet Configurations Following The XDB Base Directory Specification

Updated: 2 minute read

Background/Problem

I’ve been using Vim for years now, but was always a little annoyed by having to have the .vimrc and the .vim directory directly in my home directory. Not so long ago, I tried to clean up my home directory and moved as many program configuration files/directories as possible out of my home directory into ~/.config/. The ArchWiki page XDG Base Directory [1] was very helpful with that. While the suggested solution for Vim didn’t quite work for me (more on that later), it still helped me to get my .vimrc and .vim into ~/.config.

A few days ago I decided, that I wanted to give Neovim a shot. While I wanted to use as much of my Vim configuration as possible for my Neovim setup, I also wanted to keep them completely separated. Unfortunately after installing Neovim, it just wouldn’t load its own configuration file (~/.config/nvim/init.vim). It took my quite some time to figure out why, and how to make it work, so that both Vim and Neovim have separate configurations which are found under ~/.config/vim and ~/.config/nvim respectively.

Solution

So in order to get Vim working with a configuration that follows the XDG Base Directory Specification, I started by doing what was written on the XDG Base Directory page [1] in the ArchWiki. This involved exporting the environment variable VIMINIT in ~/.profle (well I use ~/.bash_profile, but I don’t think that should make a difference). Since back than, I already had had in mind, that I might try out Neovim in the near future, and put the following line in my ~/.bash_profile file:

export VIMINIT='if !has('nvim') | source "$XDG_CONFIG_HOME/vim/vimrc" | endif'

This was suggested in case you want to use Neovim as well. Unfortunately this didn’t work for me. When I opened Vim I got the following error messge:

Error detected while processing VIMINIT:
E121: Undefined variable: nvim
E116: Invalid arguments for function has(nvim) | \
      source "$XDG_CONFIG_HOME/vim/vimrc" | endif

Therefore I figured, I’d settle for the solution that doesn’t support Neovim and take care of it later, once I get to check out Neovim.

export VIMINIT='source "$XDG_CONFIG_HOME/vim/vimrc"'

Well that ‘later’ came sooner as expected. And with that line in my ~/.bash_profile, Neovim would source my vimrc file at startup instead of its init.vim file. After searching for a while, I came across a blog post (that is not online anymore) [2] by Jakub Łukasiewicz. He suggested to put the following line into the ~/.profile file:

export VIMINIT='if !has("nvim") | let $MYVIMRC="$XDG_CONFIG_HOME/vim/vimrc" \
    | so $MYVIMRC | endif'

While that work for Vim, Neovim didn’t source any personal configuration file at all with this line in my ~/.bash_profile. After some playing around with that VIMINIT variable, I finally found a way that works for both programs (VIM and Neovim):

export VIMINIT='if !has("nvim") | let $MYVIMRC="$XDG_CONFIG_HOME/vim/vimrc" \
    | else | let $MYVIMRC="$XDG_CONFIG_HOME/nvim/init.vim" | endif | source $MYVIMRC'

p.s.: Some Files Still in Home Directory

Yes, I still have my .bash_profile and for that matter my .bashrc and .bash_logout as well in my home directory. Unfortunatelly it doesn’t seem to be possible to get it out of there as suggested on the ArchWiki XDG Base Directory page [1]. If you do have a nice workaround let me know.

Change Log

  • 2022-09-12:

    • Fixed some typos.
    • Changed wording a little bit.
  • 2024-02-07

    • Remove link that does not work anymore.



Take care,
Andreas


References

  1. ArchWiki, “XDG Base Directory,” 23-Dec-2020. [Online]. Available at: https://wiki.archlinux.org/title/XDG_Base_Directory. [Accessed: 28-Dec-2020].
  2. J. Łukasiewicz, “Make Vim follow XDG Base Directory specification,” 13-Dec-2020. [Online]. Available at: https://blog.joren.ga/configuring/vim-xdg. [Accessed: 28-Dec-2020].

Updated:

Leave a comment