Per-project configuration for vim

Wed 03 April 2019 | tags: gitvim

For a long time I have accumulated different configurations for vim for the different open source projects I’m contributing or have contributed in the past. It became a giant unmaintained file for ftplugin/ftdetect with regexes to match the dir name and try to apply every important thing a project needs: from coding style to commands to build the source code.

After changing my machine and re-installing several softwares, I’m trying to keep my dotfiles more organized. For vim, after looking at some options available I settled in this one, that I think is very short and elegant:

" Git specific configuration
let git_path = system("git rev-parse --git-dir 2>/dev/null")
let git_vimrc = substitute(git_path, '\n', '', '') . "/vimrc"
if !empty(glob(git_vimrc))
    exec ":source " . git_vimrc

Unfortunately I don’t remember where this came from. But it allows you to have an additional vimrc to be loaded depending on the project you are. Since nowadays 99% of the projects I have are using git, it tries to load the vimrc from the git-dir of that project, which is often .git/vimrc.

I also have a ~/.vim/repo_vimrcs/ in which I keep the configuration files for projects I may have more than one tree or checkout. Inside each of those trees I only need to have a symlink to point to the “one source of truth” for configuring that project:

.git/vimrc -> ~/.vim/repo_vimrcs/linux.vimrc

In this file I can keep the coding style and also configure makeprg to do the right thing when trying to build that project. For example, this is what I have for igt, that uses meson as build system:

let &makeprg='ninja -C build'