以前买次写代码的头信息的时候都是自己写,这样太不geek了,决定使用vim编辑器自动添加头信息
在vimrc中添加以下信息
"SET Comment START
autocmd BufNewFile *.php,*.js,*.cpp,*.c,*.v,*.sv,*.m,*.sh exec ":call SetComment()" |normal 10Go
func SetComment()
if expand("%:e") == 'php'
call setline(1, '<?php')
elseif expand("%:e") == 'js'
call setline(1, '//javaScript file')
elseif expand("%:e") == 'cpp'|| expand("%:e") == 'c'
call setline(1, '//c++ file')
elseif expand("%:e") == 'v' || expand("%:e") == 'sv'
call setline(1, '//verliog file')
elseif expand("%:e") == 'm'
call setline(1, '%%matlab file')
elseif expand("%:e") == 'sh'
call setline(1, '##bash file')
endif
if expand("%:e") == 'm'
call append(1, '%**************************************************')
call append(2, '%')
call append(3, '% Filename: '.expand("%"))
call append(4, '%')
call append(5, '% Author: Ethan - Ehtan@abbswim.com')
call append(6, '% Description: ---')
call append(7, '% Create: '.strftime("%Y-%m-%d %H:%M:%S"))
call append(8, '% Last Modified: '.strftime("%Y-%m-%d %H:%M:%S"))
call append(9, '%**************************************************')
elseif expand("%:e") == 'sh'
call append(1, '#**************************************************')
call append(2, '#')
call append(3, '# Filename: '.expand("%"))
call append(4, '#')
call append(5, '# Author: Ethan - Ehtan@abbswim.com')
call append(6, '# Description: ---')
call append(7, '# Create: '.strftime("%Y-%m-%d %H:%M:%S"))
call append(8, '# Last Modified: '.strftime("%Y-%m-%d %H:%M:%S"))
call append(9, '#**************************************************')
else
call append(1, '/**************************************************')
call append(2, '*')
call append(3, '* Filename: '.expand("%"))
call append(4, '*')
call append(5, '* Author: Ethan - Ehtan@abbswim.com')
call append(6, '* Description: ---')
call append(7, '* Create: '.strftime("%Y-%m-%d %H:%M:%S"))
call append(8, '* Last Modified: '.strftime("%Y-%m-%d %H:%M:%S"))
call append(9, '**************************************************/')
endif
endfunc
map <F5> :call SetComment()<CR>:10<CR>o
"Set Comment END
"SET Last Modified Time START
func DataInsert()
call cursor(9,1)
if search ('Last Modified') != 0
let line = line('.')
if expand("%:e") == 'm'
call setline(line, '% Last Modified: '.strftime("%Y-%m-%d %H:%M:%S"))
elseif expand("%:e") == 'sh'
call setline(line, '# Last Modified: '.strftime("%Y-%m-%d %H:%M:%S"))
else
call setline(line, '* Last Modified: '.strftime("%Y-%m-%d %H:%M:%S"))
endif
endfunc
autocmd FileWritePre,BufWritePre *.php,*.js,*.cpp,*.c,*.v,*.sv,*.m,*.sh ks|call DataInsert() |'s
"SET Last Modified Time END
代码的第一段是在文件打开的同时自动添加注释信息,由于m文件和bash文件的注释比较特殊所以重新列了一堆,水平有限没想到什么好的优化代码的方法,希望有大神可以指点。第二段代码是更新修改时间。