通过修改VIM的配置文件.vimrc可以让Vim(gvim)支持自动添加作者信息,具体代码如下:
"进行版权声明的设置 "添加或更新头 map <F4> :call TitleDet()<cr>'s function AddTitle() call append(0,"/*=============================================================================") call append(1,"#") call append(2,"# Author: dantezhu - dantezhu@vip.qq.com") call append(3,"#") call append(4,"# QQ : 327775604") call append(5,"#") call append(6,"# Last modified: ".strftime("%Y-%m-%d %H:%M")) call append(7,"#") call append(8,"# Filename: ".expand("%:t")) call append(9,"#") call append(10,"# Description: ") call append(11,"#") call append(12,"=============================================================================*/") echohl WarningMsg | echo "Successful in adding the copyright." | echohl None endf "更新最近修改时间和文件名 function UpdateTitle() normal m' execute '/# *Last modified:/s@:.*$@=strftime(": %Y-%m-%d %H:%M")@' normal '' normal mk execute '/# *Filename:/s@:.*$@=": ".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=1 "默认为添加 while n < 10 let line = getline(n) if line =~ '^#s*S*Lastsmodified:S*.*$' call UpdateTitle() return endif let n = n + 1 endwhile call AddTitle() endfunction
实际上在一个C/C++/C#/JAVA文件中,只需要按下F4,那么信息就自动给你添加到文件开头啦;如果已经存在版权信息,那么vim会帮你自动更新到最新状态。
参考文献:
[1] 注:对原文进行了删减 原文地址:http://www.cnblogs.com/mfryf/p/3643349.html
[2] vim设置新建文件自动填入的文件头,以及设置编辑时间. http://blog.csdn.net/waleking/article/details/7632743