Do you wish
.vimrc were more modular?
Often times I need a project specific Vim configuration, whether it’s just a indentation/whitespace configuration (btw check out editorconfig/editorconfig-vim for
.editorconfig support), or setting up project specific makeprg and build tools, or simply loading a project specific set of plugins. To solve this problem, I have initially had a project specific clone of the
.vimrc file, but have soon realized that there is a lot of code duplication, and that these files could be much more concise if there was a way to cascade the settings from multiple
So I’ve created this little snippet, and put it in my main
.vimrc file (or
~/.config/nvim/init.vim if you prefer NeoVim):
let vimrc_filename = ".vimrc"
let tags_filename = "tags"
" Start looking for .vimrc files from the root dir
let local_path = "/"
let current_path = getcwd()
" If the current path is a child of $HOME directory, start looking from $HOME instead
if current_path =~ $HOME
let local_path = $HOME . local_path
let current_path = substitute(current_path, $HOME, '', '')
let path_parts = split(current_path, "/")
for path_part in path_parts
let local_path = local_path . path_part . "/"
if filereadable(local_path . vimrc_filename)
exe ":so " . local_path . vimrc_filename
if filereadable(local_path . tags_filename)
exe ":set tags+=" . local_path . tags_filename
unlet path_parts current_path local_path tags_filename vimrc_filename
This script simply looks for all the
.vimrc files in parent’s subdirectories and sources them all. Since it starts from the root folder (or from
$HOME) and works its way to the current directory, it favours the most recent
.vimrc files allowing them to override any settings previously set by any
.vimrc that are higher in the directory structure. As a bonus, it also loads the tags files in the same way, because my setup is weird that way.
This allows me to have a global
.vimrc file in my
$HOME directory, then another one for my personal projects
$HOME/projects/.vimrc, and even one that is more specific for a
$HOME/projects/frontend/.vimrc, and then some React specific config for my React projects in
$HOME/projects/frontend/react/.vimrc etc. You get the idea.
This comes in really handy if you need to load a different set of plugins for each of your projects, or have project specific Ultisnips snippets, or simply want to share a minimal
.vimrc config with other team members for this one specific project.
You can install it from ArtBIT/vim-modularvimrc.
Let me know what you think @artbit.