20.3.6 Updating Time Stamps Automatically

Having a time stamp in the text of a file ensures that the time the file was written will be preserved even if the file is copied or transformed in a way that loses the file system’s modification time. A time stamp may also be called a date stamp or a last modified time. You can arrange to have a time stamp in a file update automatically each time you save the file.

There are two steps to setting up automatic time stamping. First, the file needs a time stamp template. By default, the template occurs somewhere in the first eight lines and looks like this:

Time-stamp: <>

or (your choice) like this:

Time-stamp: " "

With that template in place, you can update the current buffer’s time stamp once immediately with the command M-x time-stamp. The Emacs editor will check for a template; if a template is found, Emacs will write the current date, time, author, and/or other info between the angle brackets or quotes. After the first time stamp, the line might look like this:

Time-stamp: <1993-07-06 11:05:14 terryg>

Second, configure your Emacs to run time-stamp whenever it saves a file, by adding time-stamp to before-save-hook (see Hooks). There are two ways to do this: you can

Now every time you save a file, Emacs will look for a time stamp. If the buffer has no template, time-stamp does nothing; any file that does have a time stamp will have it kept up to date automatically.