VIM通用时间戳脚本

曾经写过一个VIM的时间戳脚本,用于自动在源代码文件中添加和更新时间戳,也就是Last Change的注释。前段时间重新写.vimrc,把这个脚本又完善了一下,下载请点

PS. 最近VIM又学到不少技巧。觉得最有用的:!grep可以把visual模式下选中的行用grep工具过滤。其次可以把ii设置成命令模式和插入模式的开关。少了遥远的<Esc>效率高好多,就算不清醒,这个命令也基本上是无害的。

function! TimeStamp(...)
    let sbegin = ''
    let send = ''
    if a:0 >= 1
        let sbegin = a:1.' '
    endif
    if a:0 >= 2
        let send = ' '.a:2
    endif
    let pattern = sbegin . 'Last Change: .\+'
        \. send
    let pattern = '^\s*' . pattern . '\s*$'
    let row = search(pattern, 'n')
    let now = strftime('%Y-%m-%d %H:%M:%S',
        \localtime())
    let now = sbegin . 'Last Change: '
        \. now . send
    if row == 0
        call append(0, now)
    else
        call setline(row, now)
    endif
endfunction

au BufWritePre _vimrc,*.vim   call TimeStamp('"')
au BufWritePre *.c,*.h        call TimeStamp('//')
au BufWritePre *.cpp,*.hpp    call TimeStamp('//')
au BufWritePre *.cxx,*.hxx    call TimeStamp('//')
au BufWritePre *.java         call TimeStamp('//')
au BufWritePre *.rb           call TimeStamp('#')
au BufWritePre *.py           call TimeStamp('#')
au BufWritePre Makefile       call TimeStamp('#')
au BufWritePre *.php
    \call TimeStamp('<?php //', '?>')
au BufWritePre *.html,*htm
    \call TimeStamp('<!--', '-->')

Related Posts

on November 11th, 2008 | 2 Comments »

2 Responses to “VIM通用时间戳脚本”

  1. cat says:

    非常好~谢谢~建议更新亚~
    检测LastChange的方式应该更强大一点~

    现在不支持类似于
    /*
    * Last Change:
    */
    的格式。如果填入TimeStamp(‘/\*’, ‘\*/’)的转义符格式,最终输出又会有问题
    而且必须在单行,如果在注释体的任意部分都能检测到就好了!
    thanks

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>