Vim 写 C/C++ 的配置

.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 (definezxh@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'
  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值