Skip to content

Instantly share code, notes, and snippets.

@alwc
Forked from jelera/timestamp.vim
Created May 10, 2020 17:20
Show Gist options
  • Save alwc/7c7565a54f0b6c45a78e5ff29ee8c5d2 to your computer and use it in GitHub Desktop.
Save alwc/7c7565a54f0b6c45a78e5ff29ee8c5d2 to your computer and use it in GitHub Desktop.
It auto updates the timestamp in lines within the 20 first lines, matching "Last modified, Updated, Changed, etc."
" Append this to your vimrc file
"""""""""""""""""""""""""""""""""
" auto-update the timestamp right before saving a file
" The Timestamp format is : Sat 07 Dec 2013 12:51:00 AM CST
" Within the 20 first lines, the matched lines are :
" Last [Cc]hange(d)
" Changed
" Last [Mm]odified
" Modified
" Last [Uu]pdate(d)
autocmd! BufWritePre * :call s:timestamp()
" to update timestamp when saving if its in the first 20 lines of a file
function! s:timestamp()
let pat = '\(\(Last\)\?\s*\([Cc]hanged\?\|[Mm]odified\|[Uu]pdated\?\)\s*:\s*\).*'
let rep = '\1' . strftime("%a %d %b %Y %I:%M:%S %p %Z")
call s:subst(1, 20, pat, rep)
endfunction
" subst taken from timestamp.vim
" {{{ 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
" }}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment