您的位置:首页 > 编程语言 > C语言/C++

Vim 写 C/C++ 的配置

2018-03-24 14:15 190 查看

准备重装系统,把一些当前使用的
.vimrc
上传一下,后续继续更新

基本的配置,缩进显示行号等

给每个 C/C++ 文件添加头部,显示作者,文件创建时间

F5 编译 F6 执行 C/C++源代码

Ctrl + F 利用用 astyle 格式化代码

使用 clang_complete 对代码进行补全

set tabstop=4
set expandtab
set softtabstop=4
set shiftwidth=4
set cindent
set number
syntax on

"-- new file .h .c cpp, add file header ----------------------------
autocmd BufNewFile *.[ch],*.cpp exec ":call SetTitle()"

func SetTitle()
call setline(1,"/*")
call append(line("."), " * Created by zxh on ".strftime("%Y/%m/%d")." . All rights reserved.")
call append(line(".")+1, " */")
call append(line(".")+2, "")
endfunc
" ------------------------------------------------------------------

" C/C++/Go---------------------- F5 Compile, F6 Execute ------------
func! CompileC()
exec "w"
let compilecmd="!clang "
let compileflag=" "
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! CompileGolang()
exec "w"
let compilecmd="!go run "
let compileflag=" %< "
exec compilecmd." % ".compileflag
endfunc

func! CompileCode()
exec "w"
if &filetype == "cpp"
exec "call CompileCpp()"
elseif &filetype == "c"
exec "call CompileC()"
elseif &filetype == "go"
exec "call CompileGolang()"
endif
endfunc

func! RunResult()
exec "w"
if &filetype == "cpp"
exec "! ./a.out"
elseif &filetype == "c"
exec "! ./a.out"
endif
endfunc

map <F5> :call CompileCode()<CR>
imap <F5> <ESC> :call CompileCode()<CR>
vmap <F5> <ESC> :call CompileCode()<CR>
map <F6> :call RunResult()<CR>
"-------------------------------------------------------------------

" use Astyle to format code, <Ctrl + F> ----------------------------
func! CoderFormat()
exec "w"
if &filetype == "c" || &filetype == "h"
exec "!astyle -n --style=kr -s8 %"
elseif &filetype == "cpp"
exec "!astyle -n --style=google %"
endif
endfunc
map <C-F> :call CoderFormat() <CR>
" ------------------------------------------------------------------

" clang_complete ---------------------------------------------------
let g:clang_complete_copen=1
let g:clang_use_library =1
" ------------------------------------------------------------------
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  vim c/c++ .vimrc