The Accidental Rubyist

invalid byte sequence in UTF-8

Archive for November 2012

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

Written by totalrecall

November 29, 2012 at 10:30 am

Posted in vim