Skip to content

Instantly share code, notes, and snippets.

@ayosec
Created August 18, 2010 17:41
Show Gist options
  • Save ayosec/535542 to your computer and use it in GitHub Desktop.
Save ayosec/535542 to your computer and use it in GitHub Desktop.
" Put this file in ~/.vim/plugin/local_vimrc.vim
" When open a file (buffer) this script will find a .vimrc in the file's directory and in its parents.
function! LoadLocalVimrc()
let paths = split(expand("%:p:h"), "/")
while len(paths) > 0
call add(paths, ".vimrc")
let source_name = "/" . join(paths, "/")
if filereadable(source_name) && $MYVIMRC != source_name
exe("source " . source_name)
return 1
endif
call remove(paths, -2, -1)
endwhile
return 0
endfunction
augroup localvimrcaugroup
au!
au! BufRead,BufNewFile * call LoadLocalVimrc()
augroup END
call LoadLocalVimrc()
@ayosec
Copy link
Author

ayosec commented Aug 18, 2010

The function will fail when open a file using an absolute path.

@ayosec
Copy link
Author

ayosec commented Dec 10, 2011

The bug of the previous comment is fixed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment