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:
parent
7f1900d484
commit
67339be8b9
1 changed files with 39 additions and 4 deletions
43
vimrc
43
vimrc
|
@ -153,7 +153,8 @@ nnoremap <C-l> <C-w>l
|
||||||
nnoremap <silent> <C-n> :bn<CR>
|
nnoremap <silent> <C-n> :bn<CR>
|
||||||
nnoremap <silent> <C-p> :bp<CR>
|
nnoremap <silent> <C-p> :bp<CR>
|
||||||
|
|
||||||
function! <SID>StripTrailingWhitespace()
|
|
||||||
|
function! <SID>strip_trailing_whitespace()
|
||||||
" save last search
|
" save last search
|
||||||
let _s=@/
|
let _s=@/
|
||||||
" save cursor position
|
" save cursor position
|
||||||
|
@ -166,12 +167,41 @@ function! <SID>StripTrailingWhitespace()
|
||||||
call cursor(l, c)
|
call cursor(l, c)
|
||||||
endfunction
|
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=','
|
let mapleader=','
|
||||||
" strip all trailing whitespace in the current file
|
" strip all trailing whitespace in the current file
|
||||||
nnoremap <silent> <leader>W :call <SID>StripTrailingWhitespace()<CR>
|
nnoremap <silent> <leader>W :call <SID>strip_trailing_whitespace()<CR>
|
||||||
" edit and source my .vimrc
|
" (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>ev :e $MYVIMRC<CR>
|
||||||
nmap <silent> <leader>sv :source $MYVIMRC<CR>
|
nmap <silent> <leader>sv :source $MYVIMRC<CR>
|
||||||
|
nmap <silent> <leader>eP :e <SID>find_project_file()<CR>
|
||||||
|
|
||||||
" hide search terms
|
" hide search terms
|
||||||
nmap <silent> <leader><space> :setlocal invhlsearch<CR>
|
nmap <silent> <leader><space> :setlocal invhlsearch<CR>
|
||||||
" find all
|
" find all
|
||||||
|
@ -209,9 +239,14 @@ if has('autocmd')
|
||||||
\ exe "normal! g`\"" |
|
\ exe "normal! g`\"" |
|
||||||
\ endif
|
\ 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
|
" Clean whitespace before saving: Python, C, HTML, and Objective-C
|
||||||
autocmd BufWritePre *.py,*.h,*.c,*.html,*.m,*.mm,*.cc,*.hh
|
autocmd BufWritePre *.py,*.h,*.c,*.html,*.m,*.mm,*.cc,*.hh
|
||||||
\ call <SID>StripTrailingWhitespace()
|
\ call <SID>strip_trailing_whitespace()
|
||||||
endif
|
endif
|
||||||
|
|
||||||
if has('unix')
|
if has('unix')
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue