作为一个运维在编写自动化脚本时,希望在脚本头部添加自己的作者信息,又或者版权信息,但是每写一个脚本就添加一遍注释,就显得自己很呆,本身工作就是做的自动化,结果还是劳心劳肺。

可以在vim中设置自动添加头部信息,将以下代码复制到/etc/vimrc文件的末尾即可将全局中的vim,也可以添加到用户目录下的.vimrc文件末尾(用户目录下没有文件,创建直接粘贴进去就可以)。

" 当新建 .h .c .hpp .cpp .mk .sh等文件时自动调用SetTitle 函数
autocmd BufNewFile *.[ch],*.hpp,*.cpp,Makefile,*.mk,*.sh exec ":call SetTitle()"
" 加入注释
func SetComment()
        call append(line("."),   "* Copyright (C) ".strftime("%Y")." xxx All Rights Reserved.")   " 版权信息,xxx改为公司名或者作者名
        call append(line(".")+1, "* ")
        call append(line(".")+2, "* File Name: ".expand("%:t"))   " 脚本文件名
        call append(line(".")+3, "* Author   : xxx   https://www.feiyiblog.com")     " xxx修改为作者信息,网站可以删除
        call append(line(".")+4, "* Creation Date: ".strftime("%Y-%m-%d"))           " 文件创建日期
        call append(line(".")+5, "* INFO     :")                                     " 脚本说明信息
        call append(line(".")+7, "*")
endfunc
" 加入shell,Makefile注释
func SetComment_sh()
        call setline(3, "# Copyright (C) ".strftime("%Y")." xxx All Rights Reserved.")  "版权信息,xxx改为公司或者作者名
        call setline(4, "# ")
        call setline(5, "# File Name: ".expand("%:t"))   " 脚本文件名
        call setline(6, "# Author   : xxx   https://www.feiyiblog.com")     " xxx改为作者信息
        call setline(7, "# Creation Date: ".strftime("%Y-%m-%d"))           " 文件创建日期
        call setline(8, "# INFO     :")                                     " 脚本说明信息
        call setline(9, "# ")
endfunc
" 定义函数SetTitle,自动插入文件头
func SetTitle()
        if &filetype == 'make'
                call setline(1,"")
                call setline(2,"")
                call SetComment_sh()

        elseif &filetype == 'sh'
                call setline(1,"#!/bin/bash")  " 当以sh结尾的文件时,自动添加解释器
                call setline(2,"")
                call SetComment_sh()

        else
             call SetComment()
             if expand("%:e") == 'hpp'
                  call append(line(".")+10, "#ifndef _".toupper(expand("%:t:r"))."_H")
                  call append(line(".")+11, "#define _".toupper(expand("%:t:r"))."_H")
                  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"))."_H")

             elseif expand("%:e") == 'h'
                call append(line(".")+10, "#pragma once")
             elseif &filetype == 'c'
                call append(line(".")+10,"#include \"".expand("%:t:r").".h\"")
             elseif &filetype == 'cpp'
                call append(line(".")+10, "#include \"".expand("%:t:r").".h\"")
             endif
        endif
endfun

效果如下:

$ vim 8.sh
#!/bin/bash

# Copyright (C) 2021 FeiYi All Rights Reserved.
# 
# File Name: 8.sh
# Author   : FeiYi   https://www.feiyiblog.com
# Creation Date: 2021-04-07
# INFO     :
# 

评论




正在载入...
PoweredHexo
HostedAliyun
DNSAliyun
ThemeVolantis
UV
PV
BY-NC-SA 4.0