Vim 寫 C/C++ 的配置
阿新 • • 發佈:2019-01-08
.vimrc 2018/08/08 更新
- 基本的配置,縮排顯示行號等
- 給每個 C/C++ 檔案新增頭部,顯示作者,檔案建立時間
- F5 編譯執行 C/C++原始碼
- Ctrl + F 利用用 astyle 格式化程式碼
- 使用 clang_complete 對程式碼進行補全
注: clang_complete 需要vim支援python,可以使用vim –version檢視是否有+python(3)的選項,沒有的話重灌vim
set number
syntax on
" -- New file .h .c .cpp, add file header --
autocmd BufNeWFile *.[ch],*.cpp exec " :call CFileHeader()"
func CFileHeader()
call setline(1, "// File: ".strftime(expand('%d')))
call append(line("."), "// Author: xianhui ([email protected]163.com)")
call append(line(".")+1, "// Date: " .strftime("%Y/%m/%d %H:%M:%S"))
call append(line(".")+2, "")
exec " $"
endfunc
" -- .c .cpp .h file indent --
autocmd BufEnter *.[ch],*.cpp exec ":call CFileIndent()"
func CFileIndent()
set cindent
set tabstop=4
set softtabstop=4
set expandtab
set shiftwidth=4
endfunc
" -- Use Astyle to format code, <Ctrl + F> --
func CodeFormat()
exec " w"
if &filetype == "h"
exec "!astyle -n --style=kr -s4 %"
elseif &filetype == "c"
exec "!astyle -n --style=kr -s4 %"
elseif &filetype == "cpp"
exec "!astyle -n --style=google %"
endif
endfunc
map <C-F> :call CodeFormat() <CR>
" -- F5 compile and run code --
func CompileC()
exec "w"
let compilecmd = "!clang "
if search("math\.h") != 0
let compileflag .= "-lm"
endif
exec compilecmd." % ".compileflag
endfunc
func CompileCpp()
exec "w"
let compilecmd = "!clang++ "
let compileflag = ""
exec compilecmd." % ".compileflag
endfunc
func CompileRun()
exec "w"
if &filetype == "c"
exec "call CompileC()"
elseif &filetype == "cpp"
exec "call CompileCpp()"
endif
exec "! ./a.out"
endfunc
map<F5> <ESC> :call CompileRun() <CR>
" use clang_complete complete code
let g:clang_library_path = '/usr/lib/llvm-3.8/lib'
"let g:clang_library_path = 'usr/lib64/libclang.so.3.8'