Vim: add {find,source}_project_file() functions

- On VimEnter, look for and source (if found) a project.vim file containing
  settings specific to this project. Files are found by looking in the current
  working directory and searching up the tree until the root.
This commit is contained in:
Eryn Wells 2013-01-13 10:12:34 -08:00
parent 7f1900d484
commit 67339be8b9

43
vimrc
View file

@ -153,7 +153,8 @@ nnoremap <C-l> <C-w>l
nnoremap <silent> <C-n> :bn<CR>
nnoremap <silent> <C-p> :bp<CR>
function! <SID>StripTrailingWhitespace()
function! <SID>strip_trailing_whitespace()
" save last search
let _s=@/
" save cursor position
@ -166,12 +167,41 @@ function! <SID>StripTrailingWhitespace()
call cursor(l, c)
endfunction
function! <SID>find_project_file()
let l:dir = getcwd()
" Search up the path, starting at the current working directory, for a
" project.vim file and return the path to it, if it exists.
while l:dir != '/'
let l:project_file = l:dir . '/project.vim'
if filereadable(l:project_file)
return l:project_file
endif
let l:dir = fnamemodify(l:dir, ':h')
endwhile
return ''
endfunction
function! <SID>source_project_file()
let l:project_file = <SID>find_project_file()
if l:project_file != ''
exec 'source ' . l:project_file
endif
endfunction
let mapleader=','
" strip all trailing whitespace in the current file
nnoremap <silent> <leader>W :call <SID>StripTrailingWhitespace()<CR>
" edit and source my .vimrc
nnoremap <silent> <leader>W :call <SID>strip_trailing_whitespace()<CR>
" (Re)load a project.vim file
nnoremap <leader>P :exec 'edit ' . <SID>source_project_file()<CR>
" edit and source .vimrc and project.vim files
nmap <silent> <leader>ev :e $MYVIMRC<CR>
nmap <silent> <leader>sv :source $MYVIMRC<CR>
nmap <silent> <leader>eP :e <SID>find_project_file()<CR>
" hide search terms
nmap <silent> <leader><space> :setlocal invhlsearch<CR>
" find all
@ -209,9 +239,14 @@ if has('autocmd')
\ exe "normal! g`\"" |
\ endif
" All my projects are in the ~/Code directory. Look for and source a
" project.vim file if one exists.
autocmd VimEnter ~/Code/*
\ call <SID>source_project_file()
" Clean whitespace before saving: Python, C, HTML, and Objective-C
autocmd BufWritePre *.py,*.h,*.c,*.html,*.m,*.mm,*.cc,*.hh
\ call <SID>StripTrailingWhitespace()
\ call <SID>strip_trailing_whitespace()
endif
if has('unix')