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

版权声明:欢迎转发,转发请标明出处 https://blog.csdn.net/weixin_39465823/article/details/81147873

 必须是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:这个地方是在文件关闭时,调用修改函数时间。

 

展开阅读全文