给源码文件头部添加注释信息,有很多方法,这里介绍一种在文件打开时,自动给文件添加头部信息的方法,网上很多文章,都只能在文件打开后,通过映射快捷键到指定函数来添加头部信息,本文章中的部分函数,有参考此博客http://www.cnblogs.com/mfryf/p/3643349.html,但具体思路是自己所想。
具体.vimrc中的配置代码如下:
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
"打开文件时自动添加标题
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
autocmd BufRead *.c,*.cc,*.cpp exec ":call SetTitle()"
function AddTitle()
call append(0,"/********************************************************************************")
call append(1,"** ")
call append(2,"** Email : \t\t jiashadidai110@gmail.com")
call append(3,"** ")
call append(4,"** Author: \t\t contestjia")
call append(5,"** ")
call append(6,"** Filename: \t ".expand("%:t"))
call append(7,"** ")
call append(8,"** Last modified: ".strftime("%Y-%m-%d %H:%M:%S"))
call append(9,"** ")
call append(10,"********************************************************************************/")
"redraw可以去掉提示信息 Press Enter or type command to continue
"这个redraw意义不大
redraw
if &filetype == 'c'
call append(11,"#include <stdio.h>")
endif
if &filetype == 'cc'
call append(11,"#include <iostream>")
call append(11,"#include <stdio.h>")
endif
if &filetype == 'cc'
call append(11,"#include <iostream>")
endif
if &filetype == 'cpp'
call append(11,"#include <iostream>")
endif
echohl WarningMsg | echo "Successful in adding the title." | echohl none
endf
function UpdateTitle()
execute '/\*\* *Last modified:/s@:.*$@\=strftime(": %Y-%m-%d %H:%M:%S")@ '
execute '/\*\* *Filename:/s@:.*$@\=":\t ".expand("%:t")@ '
"redraw可以去掉提示信息 Press Enter or type command to continue
"可以去掉redraw感受一下不同之处
redraw
echohl WarningMsg | echo "Successful in updating the title." | echohl None
endf
func SetTitle()
let n = 1
while n < 10
let line = getline(n)
if line =~'^\*\*\s*\S*Last\smodified:\S*.*$'
call UpdateTitle()
return
endif
let n = n + 1
endwhile
call AddTitle()
return
endfunc
同样,如果头部注释信息已经存在,则只会更新当前修改时间,不会重复进行添加。