diff --git a/vimrc b/vimrc index a024862..c4e2fad 100644 --- a/vimrc +++ b/vimrc @@ -153,7 +153,8 @@ nnoremap l nnoremap :bn nnoremap :bp -function! StripTrailingWhitespace() + +function! strip_trailing_whitespace() " save last search let _s=@/ " save cursor position @@ -166,12 +167,41 @@ function! StripTrailingWhitespace() call cursor(l, c) endfunction + +function! 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! source_project_file() + let l:project_file = 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 W :call StripTrailingWhitespace() -" edit and source my .vimrc +nnoremap W :call strip_trailing_whitespace() +" (Re)load a project.vim file +nnoremap P :exec 'edit ' . source_project_file() + +" edit and source .vimrc and project.vim files nmap ev :e $MYVIMRC nmap sv :source $MYVIMRC +nmap eP :e find_project_file() + " hide search terms nmap :setlocal invhlsearch " 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 source_project_file() + " Clean whitespace before saving: Python, C, HTML, and Objective-C autocmd BufWritePre *.py,*.h,*.c,*.html,*.m,*.mm,*.cc,*.hh - \ call StripTrailingWhitespace() + \ call strip_trailing_whitespace() endif if has('unix')