diff options
Diffstat (limited to 'vimrcs/codecmd.vim')
-rw-r--r-- | vimrcs/codecmd.vim | 21 |
1 files changed, 10 insertions, 11 deletions
diff --git a/vimrcs/codecmd.vim b/vimrcs/codecmd.vim index 4add725..179e68e 100644 --- a/vimrcs/codecmd.vim +++ b/vimrcs/codecmd.vim | |||
@@ -21,16 +21,16 @@ autocmd FileType xml set omnifunc=xmlcomplete#CompleteTags | |||
21 | autocmd FileType php set omnifunc=phpcomplete#CompletePHP | 21 | autocmd FileType php set omnifunc=phpcomplete#CompletePHP |
22 | autocmd FileType c set omnifunc=ccomplete#Complete | 22 | autocmd FileType c set omnifunc=ccomplete#Complete |
23 | 23 | ||
24 | " 要求格式化后的不要移动光标,并不要删除换行符和空行,保留原来的视图 | 24 | " gg=G的 |
25 | autocmd BufReadPost,BufWritePre *.html,*.vim,*.sh,*.py normal! g1G=G`"z<CR> | 25 | nnoremap gg=G :call PreserveCursorAndFormat()<CR> |
26 | " 在保存时自动使用clang-format格式化代码 | 26 | function! PreserveCursorAndFormat() |
27 | augroup FormatAutocmd | 27 | let l:save_pos = getpos(".") |
28 | autocmd! | 28 | let l:save_view = winsaveview() |
29 | autocmd BufWritePre *.c,*.cpp,*.h :let save_cursor = getpos(".") | 29 | normal! gg=G |
30 | autocmd BufWritePre *.c,*.cpp,*.h :normal! ggVG | 30 | call setpos('.', l:save_pos) |
31 | autocmd BufWritePre *.c,*.cpp,*.h :ClangFormat | 31 | call winrestview(l:save_view) |
32 | autocmd BufWritePost *.c,*.cpp,*.h :call setpos('.', save_cursor) | 32 | endfunction |
33 | augroup END | 33 | |
34 | 34 | ||
35 | command! -nargs=0 ClangFormat :call ClangFormat() | 35 | command! -nargs=0 ClangFormat :call ClangFormat() |
36 | autocmd FileType c,h,cpp,hpp,cc nnoremap <buffer> gg=G :ClangFormat<CR> | 36 | autocmd FileType c,h,cpp,hpp,cc nnoremap <buffer> gg=G :ClangFormat<CR> |
@@ -98,6 +98,5 @@ func SetTitle() | |||
98 | call append(line(".")+9, "") | 98 | call append(line(".")+9, "") |
99 | endif | 99 | endif |
100 | endif | 100 | endif |
101 | " 光标移动到文件末尾 | ||
102 | normal G | 101 | normal G |
103 | endfunc | 102 | endfunc |