Contents

Vim's internal strftime() function (:help strftime()) returns a date/time string formatted in a way you specify with a format string. Most systems support strftime(), but some don't. To store the return value of the function, the "= register (:help "=) is used. Here's a bunch of examples:

Press F5 in normal mode or in insert mode to insert the current datestamp: :help i_CTRL-R

In the example above, the uppercase P at the end inserts before the current character, which allows datestamps inserted at the beginning of an existing line. Other 'put' commands may be more useful for you: :help p:help P:help gp:help gP

That way a string of the form Aug 13, 2001 14:19:50 is embedded in the text, and it will be updated to the current date and time automatically, every time the file is saved (the ...DATE... stuff is an HTML comment and won't appear in an HTML document).

An XML file may contain a line like the following (the value is a date/time given as the number of milliseconds past the epoch):

<property name='p2.timestamp' value='1333222444000'/>

With the code below in your vimrc, enter :UpdateTimestamps to update the value to the current time in each instance of a tag with the format shown above. The code uses Vim's strftime() which requires support by the operating system, so this will only work on systems which handle %s (the number of seconds since the start of 1970-01-01; should be available on Unix systems but not Windows).

It is not necessary to type the full command: just type :Up then press Tab for command completion. Using a command allows updating to be easily applied to multiple buffers, for example, entering :bufdo UpdateTimestamps would update all tags in all buffers.

When manually editing a zone file for a DNS name server, the serial number in the SOA record needs to be updated. Often a ten-digit number is used, consisting of a timestamp of the form "YYYYMMDD" and a two-digit version number. For example, "2009042101" might represent update number 01 on 21 April 2009.

The following mapping finds the next 10-digit number, and replaces it with a timestamp + "00", and shows the original number in the message line (so you can see what change occurred):

For example, if the date today is 21 April 2009 and the next ten-digit number after the cursor is "2008123002", pressing the F8 key would change the number to "2009042100", and would display "2008123002" in the message line at the bottom of the window.

The command searches for \< (beginning word), followed by 10 digits, followed by \> (end word). The ce changes to the end of the word (deleting the number to the unnamed register), then inserts (Ctrl-r) the value of register = (which evaluates the following expression).