Vim Tips Wiki
Tip 789 Printable Monobook Previous Next

created September 13, 2004 · complexity basic · author Breadman · version 6.0

I keep forgetting to change the dates in my file headers. Even though it isn't extremely important before the release time, I found it helpful to automate the process. The following is tuned more for Rebol headers than anything else, but it works for most types of files. As a bonus, it increments the last part of a version field; that can be commented out if it isn't desired.

I should probably search for the end of headers marked by comment leaders (such as Vim scripts), but haven't gotten around to it. It also fails to find version numbers that don't terminate at the end of the line.

"Redate file headers automatically
autocmd BufWritePre * call RedateHeader()

function! RedateHeader()
  " Mark the current position, and find the end of the header (if possible)
  silent! normal! msHmtgg$%
  let lastline = line('.')
  if lastline == 1
    " Header not found, so use fifteen lines or the full file
    let lastline = Min(15, line('$'))
  " Replace any timestamps discovered, in whatever format
  silent! execute '1,' . lastline . 's/\m\%(date\|changed\?\|modifi\w\+\):\s\+"\?\zs\%(\a\|\d\|[/, :-]\)*/\=strftime("%d-%b-%Y")/ie'
  " Increment the version marker
  silent! execute '1,' . lastline . "g/[Vv]ersion:/normal! $\<C-a>"
  " Restore the marked position
  silent! normal! 'tzt`s

function! Min(number, ...)
  let result = a:number
  let index = a:0
  while index > 0
    let result = (a:{index} > result) ? result : a:{index}
    let index = index - 1
  return result

Cream AddOn[]

Cream has an add-on that uses a dialog to do timestamps:

It provides eight date format options, picks up the timezone and provides a global to change/provide it if your OS doesn't (Windows). The default button is set to the last used, and if you don't like the default stamp tag "Updated:", you can change the global to make it anything you want. Whitespace is also preserved after the tag.

Cream also has a sister add-on that does a similar thing with the filename: