Skip to content

Instantly share code, notes, and snippets.

@rkumar
Created November 29, 2012 04:54
Show Gist options
  • Save rkumar/4166881 to your computer and use it in GitHub Desktop.
Save rkumar/4166881 to your computer and use it in GitHub Desktop.
timestamp auto-updating in files
" Add this to your vimrc file
" auto-update "Last update: " if present whenever saving file
autocmd! BufWritePre * :call s:timestamp()
" to update timestamp when saving if its in the first 5 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment