vim创建程序文件时自动添加头部注释的方法

$ vim ~/.vimrc
然后再最后面添加即可

/*================================================================
*   Copyright (C) 2020ゞゞゞ All rights reserved.
*   
*   @file       :Producer.hh
*   @author     :ゞゞゞ
*   @date       :2020年06月06日 22:29:18
*   @description:
*
================================================================*/
" 当新建 .h .c .hpp .hh .cpp .cc .mk .sh等文件时自动调用SetTitle 函数
autocmd BufNewFile *.[ch],*.hpp,*.hh,*.cpp,*.cc,Makefile,*.mk,*.sh exec ":call SetTitle()"
" 加入注释
func SetComment()
        call setline(1,"/*================================================================")
        call append(line("."),   "*   Copyright (C) ".strftime("%Y")." ゞゞゞ All rights reserved.")
        call append(line(".")+1, "*   ")
        call append(line(".")+2, "*   @file       :".expand("%:t"))
        call append(line(".")+3, "*   @author     :ゞゞゞ")
        call append(line(".")+4, "*   @date       :".strftime("%Y年%m月%d日 %H:%M:%S"))
        call append(line(".")+5, "*   @description:")
        call append(line(".")+6, "*")
        call append(line(".")+7, "================================================================*/")
        call append(line(".")+8, "")
        call append(line(".")+9, "")
endfunc
" 加入shell,Makefile注释
func SetComment_sh()
        call setline(3, "#================================================================")
        call setline(4, "#   Copyright (C) ".strftime("%Y")." Sangfor Ltd. All rights reserved.")
        call setline(5, "#   ")
        call setline(6, "#   @file       :".expand("%:t"))
        call setline(7, "#   @author     :ゞゞゞ")
        call setline(8, "#   @date       :".strftime("%Y年%m月%d日 %H:%M:%S"))
        call setline(9, "#   @description:")
        call setline(10, "#")
        call setline(11, "#================================================================")
        call setline(12, "")
        call setline(13, "")
endfunc
" 定义函数SetTitle,自动插入文件头
func SetTitle()
        if &filetype == 'make'
                call setline(1,"")
                call setline(2,"")
                call SetComment_sh()
        elseif &filetype == 'sh'
                call setline(1,"#!/system/bin/sh")
                call setline(2,"")
                call SetComment_sh()
 
        else
                call SetComment()
                if expand("%:e") == 'hpp'
                        call append(line(".")+10, "#ifndef __".toupper(expand("%:t:r"))."_HPP__")
                        call append(line(".")+11, "#define __".toupper(expand("%:t:r"))."_HPP__")
                        call append(line(".")+12, "#ifdef __cplusplus")
                        call append(line(".")+13, "extern \"C\"")
                        call append(line(".")+14, "{")
                        call append(line(".")+15, "#endif")
                        call append(line(".")+16, "")
                        call append(line(".")+17, "#ifdef __cplusplus")
                        call append(line(".")+18, "}")
                        call append(line(".")+19, "#endif")
                        call append(line(".")+20, "#endif //".toupper(expand("%:t:r"))."_HPP")

                elseif expand("%:e") == 'hh'
                        call append(line(".")+10, "#ifndef __".toupper(expand("%:t:r"))."_HPP__")
                        call append(line(".")+11, "#define __".toupper(expand("%:t:r"))."_HPP__")
                        call append(line(".")+12, "#ifdef __cplusplus")
                        call append(line(".")+13, "extern \"C\"")
                        call append(line(".")+14, "{")
                        call append(line(".")+15, "#endif")
                        call append(line(".")+16, "")
                        call append(line(".")+17, "#ifdef __cplusplus")
                        call append(line(".")+18, "}")
                        call append(line(".")+19, "#endif")
                        call append(line(".")+20, "#endif //".toupper(expand("%:t:r"))."_HH")
 
                elseif expand("%:e") == 'h'
                        call append(line(".")+10, "#pragma once")
                elseif &filetype == 'c'
                        call append(line(".")+10,"#include \"".expand("%:t:r").".h\"")
                elseif &filetype == 'cc'
                        call append(line(".")+10, "#include \"".expand("%:t:r").".hh\"")
                elseif &filetype == 'cpp'
                        call append(line(".")+10, "#include \"".expand("%:t:r").".hpp\"")
                endif
        endif
endfun

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值