vim F7添加作者信息,文件修改完更改最后时间

 必须是vim,不能是vi。

再更新,使用函数():

set nu
set nocp
set tabstop=4
set softtabstop=4
set shiftwidth=4
set noexpandtab
syntax on
function FileHead_C()
	call append( 0,"/***************************************************")
	call append( 1,"##filename      : ".expand("%:t")                    )
	call append( 2,"##author        : GYZ                               ")
	call append( 3,"##e-mail        : 1746902011@qq.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,"                                                    ")
	call append(13,"int main(int argc,char *argv[])                     ")
	call append(14,"{                                                   ")
	call append(15,"                                                    ")
	call append(16,"	return 0;                                       ")
	call append(17,"}                                                   ")
	call append(18,"                                                    ")
	call append(19,"                                                    ")
endfunction
function FileHead_SH()
	call append(0,"#!/bin/bash")
	call append(1,"#")
	call append(2,"")
	call append(3,"")
	call append(4,"exit 0")
endfunction
map <F7> :call FileHead()<CR>5k
function FileHead()
	if &filetype == 'c'
		call FileHead_C()
	elseif &filetype == 'sh'
		call FileHead_SH()
	endif
	echo
	
endfunction
 
function SetLastModifiedTimes()
	let line = getline(6)
	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(6,repl)
	endif
endfunction
autocmd BufWrite *.c call SetLastModifiedTimes()

语法高亮语句, syntax on

更新一下:

A代码是新更新的,我发现就是我把别人代码粘贴过来,别人代码和我代码的文件头不一样,所以会导致第六行last modified会覆盖别人代码的那一行,所以我添加了判断,如果有最后修改时间,则更新,没有则不更新。

A代码:

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        : GYZ                               ")
	call append( 3,"##e-mail        : 1746902011@qq.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,"                                                    ")
	call append(13,"int main(int argc,char *argv[])                     ")
	call append(14,"{                                                   ")
	call append(15,"                                                    ")
	call append(16,"	return 0;                                       ")
	call append(17,"}                                                   ")
	call append(18,"                                                    ")
	call append(19,"                                                    ")
	echo
	
endfunction

function SetLastModifiedTimes()
	let line = getline(6)
	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(6,repl)
	endif
endfunction
autocmd BufWrite *.c call SetLastModifiedTimes()

B代码:

set nu
set nocp
set tabstop=4
set softtabstop=4
set shiftwidth=4
set noexpandtab
map <F7> :call FileHead()<CR>5k
function FileHead()
	call append( 0,"/***************************************************")
	call append( 1,"##filename      : ".expand("%:t")                    )
	call append( 2,"##author        : GYZ                               ")
	call append( 3,"##e-mail        : 1746902011@qq.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,"                                                    ")
	call append(13,"#int main(int argc,char *argv[])                    ")
	call append(14,"{                                                   ")
	call append(15,"                                                    ")
	call append(16,"	return 0;                                   ")
	call append(17,"}                                                   ")
	call append(18,"                                                    ")
	call append(19,"                                                    ")
	echo
endfunction

function SetLastModifiedTimes()
	let line = getline(6)
	let newtime = "##last modified : ".strftime("%Y-%m-%d %H:%M:%S")
	let repl = substitute(line,".*$",newtime,"g")
	call setline(6,repl)
endfunction
autocmd BufWrite *.c call SetLastModifiedTimes()

“”多方参考,亮点是  let newtime = "##last modified : ".strftime("%Y-%m-%d %H:%M:%S")。

“”因为其它方法都是寻找last modified然后去修改,我这里是直接替换这行的文字。

“”使用了substitute函数,不过网上的资料太少了,然后我去vim说明手册里去查,结果也没有收获,因为它的函数讲解和c

“”语言的不同,没有那么细。

“”第一个函数 FileHead()没什么可说的,第二个函数SetLastModifiedTimes()觉得比较实用。

“”可以终端输入##vim,然后输入:help append和:help substitute,可查看这两个函数的说明手册。

""autocmd BufNewFile *.c exec ":call FileHead()"
map <F7> :call FileHead()<CR>5k
function FileHead()
    call append( 0,"/**************************************************")
    call append( 1,"##filename      : ".expand("%:t")                   )
    call append( 2,"##author        : GYZ                              ")
    call append( 3,"##e-mail        : 1746902011@qq.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,"                                                   ")
    call append(13,"int main(int argc,char *argv[])                    ")
    call append(14,"{                                                  ")
    call append(15,"                                                   ")
    call append(16,"    return 0;                                      ")
    call append(17,"}                                                  ")
    call append(18,"                                                   ")
    call append(19,"                                                   ")
    echo
endfunction

“”-------------------------------------------------------------------------------------------------------------------------------------------------------------------

function SetLastModifiedTimes()    

“”line1:
    let line = getline(6)                        
    let newtime = "##last modified : ".strftime("%Y-%m-%d %H:%M:%S")

“”line2:
   let repl = substitute(line,".*$",newtime,"g")

    call setline(6,repl)
endfunction

“”line3:
autocmd BufWrite *.c call SetLastModifiedTimes()

line1:获取第6行的字符数据;

line2:等同于:s/.*$/aaa/g,  用aaa整体替换该行,“”.*$这个参数的怎么写,我可是真正找了一天,才偶然发现的,也就是说整体替换,全文这个地方算是个人觉得比较费劲的地方;

line3:这个地方是在文件关闭时,调用修改函数时间。

 

 

欢迎评论!

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值