Learn how to refactor long vimrc config.

Suppose that you have long .vimrc config. My config had over 400 lines before splitting it into modules. I decided to split it into the following files:

`vimrc`:          main, loads the rest of config files
`vimrc.bundles`:  plug-ins
`vimrc.augroups`: *MIME*-based config (e.g. key bindings for *Ruby* / *Rails*)
`vimrc.writing`:  setup for distractless environment

All of them are kept in ~/dotfiles/. You can also find them on my GitHub account.

After the change .vimrc has 290 lines, which is a little better. As you may know, by default Vim looks for the config in the following dotfiles:

  • ~/.vimrc
  • ~/.vim/vimrc

Up to yesterday I kept 4 symlinks:

`.vimrc`          --> `~/dotfiles/vimrc`
`.vimrc.bundles`  --> `~/dotfiles/vimrc.bundles`
`.vimrc.augroups` --> `~/dotfiles/vimrc.augroups`
`.vimrc.writing`  --> `~/dotfiles/vimrc.writing`

If you want to share the config between and among vanilla Vim, gVim, MacVim, or neovim, etc. it is not very usable. For example neovim looks up the config in ~/.config/nvim/init.vim.

As I keep the whole config in the ~/dotfiles directory, I thought it’d be better to ‘delegate’ only one, main file (vimrc) that would load the rest of files. This is what I ended up with:

`~/.vimrc`                --> `~/dotfiles/vimrc`
`~/.gvimrc`               --> `~/dotfiles/vimrc`
`~/.whatevervimrc`        --> `~/dotfiles/vimrc`
`~/.config/nvim/init.vim` --> `~/dotfiles/vimrc`

This is how ~/dotfiles/vimrc load the rest of config files:

" define paths in global variables

let g:dotfilesPath  = '~/dotfiles/'
let g:vimrcBundles  = g:dotfilesPath . 'vimrc.bundles'
let g:vimrcAugroups = g:dotfilesPath . 'vimrc.augroups'
let g:vimrcWriting  = g:dotfilesPath . 'vimrc.writing'

" load files
exe 'source ' g:vimrcBundles
exe 'source ' g:vimrcAugroups
exe 'source ' g:vimrcWriting

I believe that the must a be better way to load / manage those files, but for
now it’s another small win and does the job.