vim 自动插入作者信息

发布于 2021-10-26  469 次阅读


cp /etc/vimrc /etc/vimrc.bak20210926

在 /etc/vimrc 结尾处添加如下代码
或者cat author_info.txt >>/etc/vimrc

map <F1> ms:call AddAuthor()<cr>'s
function AddAuthor()
        let n=1
        while n < 5
                let line = getline(n)
                if line =~'^\s*\*\s*\S*Last\s*modified\s*:\s*\S*.*$'
                        call UpdateTitle()
                        return
                endif
                let n = n + 1
        endwhile
        call AddTitle()
endfunction

function UpdateTitle()
        normal m'
        execute '/* Last modified\s*:/s@:.*$@\=strftime(": %Y-%m-%d %H:%M")@'
        normal "
        normal mk
        execute '/* Filename\s*:/s@:.*$@\=": ".expand("%:t")@'
        execute "noh"
        normal 'k
        echohl WarningMsg | echo "Successful in updating the copy right." | echohl None
endfunction

        function AddTitle()
        call append(0,"# *************************************************")
        call append(1,"# Author        : Erice Jia")
        call append(2,"# Email         : erice_jia@163.com")
        call append(3,"# Last modified : ".strftime("%Y-%m-%d %H:%M"))
        call append(4,"# Filename      : ".expand("%:t"))
        call append(5,"# Description   : ")
        call append(6,"# *************************************************")
        call append(7,"#!/bin/bash")
        echohl WarningMsg | echo "Successful in adding the copyright." | echohl None
endfunction

vim 编辑相关文件时,按F3便可自动的插入作者相关信息了,插入如下

vim /root/info.txt

# *************************************************
# Author        : Erice Jia
# Email         : erice_jia@163.com
# Last modified : 2021-10-26 14:48
# Filename      : info.txt
# Description   :
# *************************************************
#!/bin/bash