在编写程序的时候,经常需要在程序开始写上程序的简要介绍和作者信息,如下:
这种信息,除了文件名和修改时间可能经常发生变化外,其他基本不变,可以在程序开始自动加入,方法就是在家目录下的.vimrc中写入:
map <F4> :call TitleDet()<cr>
function AddTitle()
call append(,"\#!/usr/bin/env bash")
call append(,"# ******************************************************")
call append(,"# Author : 90Zeng")
call append(,"# Last modified: ".strftime("%Y-%m-%d %H:%M"))
call append(,"# Email : omezengjl@gmail.com")
call append(,"# Filename : ".expand("%:t"))
call append(,"# Description : ")
call append(,"# ******************************************************")
echohl WarningMsg | echo "Successful in adding copyright." | echohl None
endf function UpdateTitle()
normal m'
execute '/# Last modified/s@:.*$@\=strftime(":\t%Y-%m-%d %H:%M")@'
normal ''
normal mk
execute '/# Filename/s@:.*$@\=":\t".expand("%:t")@'
execute "noh"
normal 'k
echohl WarningMsg | echo "Successful in updating the copyright." | echohl None
endfunction function TitleDet()
let n=
while n <
let line = getline(n)
if line =~ '^\#\s*\S*Last\smodified\S*.*$'
call UpdateTitle()
return
endif
let n = n +
endwhile
call AddTitle()
endfunction
保存之后,vi test.sh,在norm 模式下按下F4即可,效果如下:
假如下一时刻,我们将文件名称改为了test2.sh,然后需要更新上面的信息,只需要:mv test.sh test2.sh, 然后vim test2.sh,F4,会自动更新修改时间和文件名称
如果我们经常需要写的python文件,那么在.vimrc中修改相应的字符即可,如下:
效果如下:
网上已经有插件实现了上述功能(http://www.vim.org/scripts/script.php?script_id=2902),但是对于安装插件不方便的场景,可以自己手动编写上述代码