The Accidental Rubyist

invalid byte sequence in UTF-8

Auto-update timestamp in files upon Save

with one comment

If you use vim for editing files, you can stick this in your vimrc file, so whenever you save the file, "Last update" gets updated with the current time. If you enhance this script, pls let me know, so we can all benefit.

This requirement is already met by timestamp.vim, but that’s a biggish plugin, and does a lot of other updates, and I just wanted a few lines in my vimrc, and only timestamp updated.

https://gist.github.com/4166881

" auto-update last update if there's a update tag
autocmd! BufWritePre * :call s:timestamp()
" to update timestamp when saving if its in the first few lines of a file
function! s:timestamp()
let pat = '\(Last update\s*:\s*\).*'
let rep = '\1' . strftime("%Y-%m-%d %H:%M")
call s:subst(1, 5, pat, rep)
endfunction
" subst taken from timestamp.vim
" {{{1 subst( start, end, pat, rep): substitute on range start - end.
function! s:subst(start, end, pat, rep)
let lineno = a:start
while lineno <= a:end
let curline = getline(lineno)
if match(curline, a:pat) != -1
let newline = substitute( curline, a:pat, a:rep, '' )
if( newline != curline )
" Only substitute if we made a change
"silent! undojoin
keepjumps call setline(lineno, newline)
endif
endif
let lineno = lineno + 1
endwhile
endfunction

" }}}1

Advertisements

Written by totalrecall

November 29, 2012 at 10:30 am

Posted in vim

One Response

Subscribe to comments with RSS.

  1. vim rocks!

    zipizap

    December 5, 2012 at 3:44 am


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: