set expandtab " 设置tab键替换为tabstop规定的空格数 set tabstop=4 " 编辑时tab键占用的空格数 set shiftwidth=4 " 设置tab的间隔(格式化时) set softtabstop=4 " 设置退格键退格的空格数,让vim把连续的空格当作一个tab " autocmd FileType *asm,vim,*sh,makefile set tabstop=4 softtabstop=4 shiftwidth=4 noexpandtab set showmatch " 在输入括号时光标会短暂地跳到与之相匹配的括号处 set nowrap " 设置自动折行 set textwidth=500 " 设置自动换行的长度 set lbr " 设置自动换行 set foldmethod=syntax " 设置按语法折叠代码 set nofoldenable " 在打开文件时不要折叠 " 自动补全配置 autocmd FileType python set omnifunc=pythoncomplete#Complete autocmd FileType javascript set omnifunc=javascriptcomplete#CompleteJS autocmd FileType html set omnifunc=htmlcomplete#CompleteTags autocmd FileType css set omnifunc=csscomplete#CompleteCSS autocmd FileType xml set omnifunc=xmlcomplete#CompleteTags autocmd FileType php set omnifunc=phpcomplete#CompletePHP autocmd FileType c set omnifunc=ccomplete#Complete " 修改gg=G的作用 nnoremap gg=G :call PreserveCursorAndFormat() function! PreserveCursorAndFormat() let l:save_pos = getpos(".") let l:save_view = winsaveview() normal! gg=G call setpos('.', l:save_pos) call winrestview(l:save_view) endfunction command! -nargs=0 ClangFormat :call ClangFormat() autocmd FileType c,h,cpp,hpp,cc nnoremap gg=G :ClangFormat " 定义ClangFormat命令: " 当前文件的各级父目录下如果存在.clang-format,则使用 " 否则使用默认规则,缩进为4 function! ClangFormat() let clang_format = "clang-format" if !executable(clang_format) normal gg=G return endif let clang_format_file = findfile(".clang-format", expand('%:p:h') . ";") if clang_format_file != "" let clang_format = clang_format . " -style=file:" . clang_format_file else let clang_format = clang_format . " -style='{IndentWidth: 4}'" endif let save_cursor = getpos(".") let save_view = winsaveview() let save_cursor = getpos(".") let save_view = winsaveview() execute ":silent %! ".clang_format call setpos('.', save_cursor) call winrestview(save_view) endfunction " 新建.c/.cpp/.sh/.py文件,自动插入文件头 autocmd BufNewFile *.cpp,*.[ch],*.sh exec ":call SetTitle()" func SetTitle() if &filetype == 'sh' call setline(1,"#!/bin/bash") call append(line("."), "" ) call append(line(".")+1, "\##########################################################################" ) call append(line(".")+2, "\# File Name : ".expand("%")) call append(line(".")+3, "\# Encoding : utf-8") call append(line(".")+4, "\# Author : We-unite") call append(line(".")+5, "\# Email : weunite1848@gmail.com") call append(line(".")+6, "\# Created Time : ".strftime("%Y-%m-%d %H:%M:%S",localtime())) call append(line(".")+7, "\##########################################################################" ) call append(line(".")+8, "") elseif &filetype =='py' call setline(1,"#!/usr/bin/env python") call append(line("."), "" ) call append(line(".")+1, "\##########################################################################" ) call append(line(".")+2, "\# File Name : ".expand("%")) call append(line(".")+3, "\# Encoding : utf-8") call append(line(".")+4, "\# Author : We-unite") call append(line(".")+5, "\# Email : weunite1848@gmail.com") call append(line(".")+6, "\# Created Time : ".strftime("%Y-%m-%d %H:%M:%S",localtime())) call append(line(".")+7, "\##########################################################################" ) call append(line(".")+8, "") else call setline(1, "/*************************************************************************") call append(line("."), " > File Name : ".expand("%")) call append(line(".")+1, " > Encoding : utf-8") call append(line(".")+2, " > Author : We-unite") call append(line(".")+3, " > Email : weunite1848@gmail.com ") call append(line(".")+4, " > Created Time : ".strftime("%Y-%m-%d %H:%M:%S",localtime())) call append(line(".")+5, " ************************************************************************/") call append(line(".")+6, "") if &filetype == 'cpp' call append(line(".")+7, "#include ") call append(line(".")+8, "using namespace std;") call append(line(".")+9, "") elseif &filetype == 'c' call append(line(".")+7, "#include ") call append(line(".")+9, "") endif endif normal G endfunc