在ubuntu的家目录下执行命令: gedit .vimrc
将下面代码修改为自己的信息后粘贴到最后保存即可。
set nu
set nocp
set tabstop=4
set softtabstop=4
set shiftwidth=4
set noexpandtab
syntax on
map <F7> :call FileHead()<CR>5k
function FileHead()
call append( 0,"/***************************************************")
call append( 1,"## filename : ".expand("%:t") )
call append( 2,"## author : xiaoming ")
call append( 3,"## e-mail : xiaoming@163.com ")
call append( 4,"## create time : ".strftime("%Y-%m-%d %H:%M:%S") )
call append( 5,"## last modified : ".strftime("%Y-%m-%d %H:%M:%S") )
call append( 6,"## description : NA ")
call append( 7,"***************************************************/")
call append( 8,"#include <stdio.h> ")
call append( 9,"#include <stdlib.h> ")
call append(10,"#include <string.h> ")
call append(11," ")
call append(12,"int main(int argc, char *argv[]) ")
call append(13,"{ ")
call append(14," ")
call append(15," return 0; ")
call append(16,"} ")
call append(17," ")
call append(18," ")
call append(19," ")
call append(20,"/***************************************************")
call append(21,"输出结果: ")
call append(22," ")
call append(23,"***************************************************/")
echo
endfunction
function SetLastModifiedTimes()
let line = getline(5)
let newtime = "## last modified : ".strftime("%Y-%m-%d %H:%M:%S")
let repl = substitute(line,".*$",newtime,"g")
let res = search("## last modified","w")
if res
call setline(5,repl)
endif
endfunction
autocmd BufWrite *.c call SetLastModifiedTimes()
测试方法:新建一个.c文件,进入后先按F7键,生成的注释如下图所示:
可以依据自己的爱好对代码进行修改,其中 last modified 一行中前面的序号要与下面这两行的序号一致,这样才能对最近一次修改的时间进行更新。
call append( 5,"## last modified : ".strftime("%Y-%m-%d %H:%M:%S") )
let line = getline(5)
...
call setline(5,repl)