sponsor Vim development Vim logo Vim Book Ad

UpdateModDate.vim : Updates a date stamp within a file whenever it is saved.

 script karma  Rating 16/7, Downloaded by 760  Comments, bugs, improvements  Vim wiki

created by
Christian Aichinger
 
script type
utility
 
description
This script uses an autocmd hook to run a function whenever a file is saved.
The function then looks if it finds a special token (by default %DATE_TAG%) within the file, and if it's found, a datestamp on the same line is updated to the current date.

This script will run on all systems that provide date(1), so it should work on unix systems, as well as on cygwin.

If no special token is found, nothing is done at all.
If there are multiple tokens within the file, only the first one from the beginning of the buffer will be updated.

The date format and the special token are fully configureable.
 
install details
Put the file into your plugin directory (~/.vim/plugin). It will then automatically be loaded.

To see an example of the script doing it's work, just put the following line in a file and save it, the date should then be updated automatically:
%DATE_TAG% Last Modified: Sun Nov  7 21:54:49 UTC 2004.
If it works once, but not thereafter, it is because your date -u uses a different output format then that on my machine. See below on how to set the DatePattern to match your date output.

You can add whitespace, comments, whatever to that line, just do not change %DATE_TAG% or anything within the date part, and pay attention that the special date token (%DATE_TAG%) and the date are on the same line.

There are 3 variables you can set from your vimrc file:
*** UMD_DateToken: This is the string the script uses to find the correct line. It defaults to '%DATE_TAG%'
*** UMD_DateCommand: This is the command that will be executed to get the current date. It defaults to  'date -u | tr -d "\n"' (this gets the date with UTC time and removes the trailing newline.
*** UMD_DatePattern: This is a regexp that defines which part of the line with the special token should be substituted with the new date. Make sure it matches if you change the DateCommand. It defaults to '\w\+\s\+\w\+\s\+\d\+\s\+\d\{2\}:\d\{2\}:\d\{2\}\s\+UTC\s\+\d\+', which matches the date -u output.

The easy way to get a working UMD_DatePattern:
In vim use
:r!date <options-you-want>
to read the output of date with the options you want into the current buffer.
Now start a vim search and construct a pattern that matches your date string within the search (hlmatch/incsearch should be on, so you see what is currently matched). Once the pattern matches the whole date string, enclose it in '' and set UMD_DatePattern to that string in your vimrc.
 

rate this script Life Changing Helpful Unfulfilling 
script versions (upload new version)

Click on the package to download.

package script version date Vim version user release notes
UpdateModDate.vim 1.2 2004-11-08 6.0 Christian Aichinger I've changed the default UDM_DatePattern a bit, to better match different date -u outputs.
It does now match the date part less strictly, but the time part more exactly. I hope this solves the "it works once but not thereafter" problem for most people.
UpdateModDate.vim 1.1 2004-11-07 6.0 Christian Aichinger The script now starts to search for the DateToken from the very top of the file, and it will only update the first occurance.

I changed the script so that the values for DateToken, DatePattern, DateCommand aren't hardcoded any more.
You can now define UMD_DateToken, UMD_DatePattern and UMD_DateCommand in your vimrc.

The default values are:
let s:UMD_DateToken = '%DATE_TAG%'
let s:UMD_DatePattern = '\w\{3\}\s\w\{3\}\s\+\d\+\s\+\d\+:\d\+:\d\+\s\+UTC\s\+\d\+'
let UMD_DateCommand = 'date -u | tr -d "\n"'
ip used for rating: 18.223.106.100

If you have questions or remarks about this site, visit the vimonline development pages. Please use this site responsibly.
Questions about Vim should go to the maillist. Help Bram help Uganda.
   
Vim at Github