上周五Derek老师分享了精彩的Vim思维导图
(回复公众号vim
即可查看哦),今天又带来什么呢?继续往下看吧!
只需要在VIM编辑器中添加以下代码即可!
#为F4添加作者信息
map <F4> ms:call TitleDet()<cr>'s
function AddTitle()
call append(0,"##############################################")
call append(1,"#")
call append(2,"#Author: wangshenghui - wangfeng17399@163.com")
call append(3,"#")
call append(4,"#QQ:417685417")
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,"#!/bin/bash")
echohl WarningMsg | echo "Successful in adding the copyright." | echohl None
endfunction
#修改文件修改时间和文件名
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*Last\smodified:\S*.*$'
call UpdateTitle()
return
endif
let n = n + 1
endwhile
call AddTitle()
endfunction
是不是很easy呢?快来试一下吧!