Keep project specific stuff in a .project.vim file

3 Points

Paul Harper Paul Harper

8 years ago

This script will search for .project.vim, ../.project.vim, ../../.project.vim etc until it finds one. If it finds one, it will source it.

This hasn't been modified to work on Windows, but shouldn't be hard to modify. Also I'm sure there are optimizations that could be made here, but it hasn't slowed me down at all.

function! s:findFileBackward(path, filename)
    let file = a:filename
    let path = a:path
    if !strlen(path)
        let path = '.'
    while !filereadable(file)
        if path ==# '/'
            return ''
        let path = system('cd '.shellescape(path.'/..').' && echo -n $PWD')
        let file = path.'/'.a:filename
    return file

function! FindFileBackward(filename)
    " Try to resolve the file from the current buffer first
    let file = s:findFileBackward(expand('%:h'), a:filename)
    if !strlen(file)
        " Try to resolve the file from the cwd
        return s:findFileBackward(getcwd(), a:filename)
    return file

let g:project_local_vimrc = FindFileBackward('.project.vim')
if filereadable(g:project_local_vimrc)
    execute 'source ' . g:project_local_vimrc

Florian Beer

Florian Beer 8 years ago

Nice, I like the idea.

Btw. this is snippet number 100 Yay!

Pierre Penninckx

Pierre Penninckx 8 years ago

You can do something similar, without the "going up in the directory" with this:

set exrc set secure

It will search for a .vimrc file in the directory you opened vim.