开发者

VIM: how to update text when saving

I'm trying to get into the habit of using one editor (gVim 7.3 on Windows XP) for all programming and development tasks.

I'd like to update a header in any open file when I save using :w

The header looks like this (in a C file):

/* Filename: hello.c
 * Filesize: 345 bytes
 * Last Modified: Fri Feb 25, 2011  01:55PM
 */

I actually already figured out how to update Last Modified, by including the following in my _vimrc file:

" If buffer modified, update any 'Last modified: ' in the first 20 lines.
" 'Last modified: ' can have up to 10 characters before (they are retained).
" Restores cursor and window position using save_cursor variable.
function! LastModified()
  if &modified
    let save_cursor = getpos(".")
    let n = min([20, line("$")])
    keepjumps exe '1,' . n . 's#^\(.\{,10}Last Modified:\).*#\1' .
          \ strftime(' %a %b %d, %Y  %I:%M%p') . '#e'
    call histdel('search', -1)
    call setpos('.', save_cursor)
  endif
endfun

autocm开发者_开发百科d BufWritePre * call LastModified()   

My question is, using a similar approach, how can I have Filename and Filesize update as well? Thanks for your help.


Don't do it at all.

Your version control system stores the same information. If you're not using one, start.


You can add the current file name using @%:

keepjumps exe '1,' . n . 's#^\(.\{,10}Filename:\).*#\1' .
      \ ' ' . @% . '#e'

No idea about the file size, though.

Not to mention that you will actually be modifying the file size by adding the file size, so it may be impossible to get right.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜