History Report a problem
Article Edit this page Discussion

Automatically redate file headers

From Vim Tips Wiki

Jump to: navigation, search

Tip 789 Previous TipNext Tip

Created: September 13, 2004 Complexity: basic Author: Breadman Minimum version: 6.0 Karma: 1/1 Imported from: Tip#789


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('$'))
  endif
  " 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
endf

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
  endwhile
  return result
endf

[edit] Cream AddOn

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

http://cvs.sourceforge.net/viewcvs.py/cream/cream/addons/cream-timestamp.vim?view=markup

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:

http://cvs.sourceforge.net/viewcvs.py/cream/cream/addons/cream-stamp-filename.vim?view=markup

[edit] Comments


Rate this article:

Share this article:

Hubs Highlights International Sites Wikia messages
Entertainment
Gaming
Cartoons & Comics
Science Fiction
Hobbies
Sports
See all...
Grand Theft Auto
Pushing Daisies
Legend of Zelda Wiki
Terminator Wiki
Everquest II Wiki
Astronomy Wiki
German
Spanish
Chinese
Japanese
More...
Wikia is hiring for several open positions


Vote for featured Wikia!

Send this article to a friend
"Automatically redate file headers"
 
 
Hi!

I thought you'd like this page from Wikia!

http://vim.wikia.com

Come check it out!
Send confirmation