Keep project specific stuff in a .project.vim file

3 Points

Paul Harper Paul Harper

9 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 = '.'
    endif
    while !filereadable(file)
        if path ==# '/'
            return ''
        endif
        let path = system('cd '.shellescape(path.'/..').' && echo -n $PWD')
        let file = path.'/'.a:filename
    endwhile
    return file
endfunction

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)
    endif
    return file
endfunction

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

Florian Beer

Florian Beer 9 years ago

Nice, I like the idea.

Btw. this is snippet number 100 Yay!

Pierre Penninckx

Pierre Penninckx 9 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.