[linux] vim在源代码中自动添加作者信息(转载)

时间:2023-03-08 22:16:37
[linux] vim在源代码中自动添加作者信息(转载)

原文出处:

http://www.vimer.cn/2009/10/用vim在源代码中添加你的个人信息.html

vim ~/.vimrc

"进行版权声明的设置
"添加或更新头
map <F4> :call TitleDet()<cr>'s
function AddTitle()
call append(,"/*============================================
=================================")
call append(,"#")
call append(,"# Author: dantezhu - dantezhu@vip.qq.com")
call append(,"#")
call append(,"# QQ : 327775604")
call append(,"#")
call append(,"# Last modified: ".strftime("%Y-%m-%d %H:%M"))
call append(,"#")
call append(,"# Filename: ".expand("%:t"))
call append(,"#")
call append(,"# Description: ")
call append(,"#")
call append(,"===========================================
==================================*/")
echohl WarningMsg | echo "Successful in adding the copyright." | echohl None
endf
"更新最近修改时间和文件名
function UpdateTitle()
normal m'
execute '/# *Last modified:/s@:.*$@\=strftime(":\t%Y-%m-%d %H:%M")@'
normal ''
normal mk
execute '/# *Filename:/s@:.*$@\=":\t\t".expand("%:t")@'
execute "noh"
normal 'k
echohl WarningMsg | echo "Successful in updating the copy right." | echohl None
endfunction
"判断前10行代码里面,是否有Last modified这个单词,
"如果没有的话,代表没有添加过作者信息,需要新添加;
"如果有的话,那么只需要更新即可
function TitleDet()
let n=
"默认为添加
while n <
let line = getline(n)
if line =~ '^\#\s*\S*Last\smodified:\S*.*$'
call UpdateTitle()
return
endif
let n = n +
endwhile
call AddTitle()
endfunction

vim编辑文件,一般模式下按F4即可插入文件头信息,提示信息:E20: Mark not set

google结果,去掉's

map <F4> :call TitleDet()<cr>