Vim Tips Wiki
(minor reword + merge in reworked DNS update from 200901)
(Made the difference between :r!<space>vim-command and :r<space>!shell-command clear)
Line 62: Line 62:
 
On Unix-based systems, enter the following in Vim to read the output from running the <tt>date</tt> utility, inserting the result after the current line:
 
On Unix-based systems, enter the following in Vim to read the output from running the <tt>date</tt> utility, inserting the result after the current line:
 
<pre>
 
<pre>
:r!date
+
:r !date
 
</pre>
 
</pre>
   
 
Under Windows, use:
 
Under Windows, use:
 
<pre>
 
<pre>
:r!date /t
+
:r !date /t
 
</pre>
 
</pre>
   

Revision as of 11:22, 30 September 2009

Tip 97 Printable Monobook Previous Next

created 2001 · complexity basic · author newbie · version 6.0


There are a variety of ways to insert a date/time stamp. You can even have Vim automatically update an existing 'last modified' date/time when writing the file.

Using strftime()

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

:nnoremap <F5> "=strftime("%c")<CR>P
:inoremap <F5> <C-R>=strftime("%c")<CR>

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

Type dts in insert mode to expand to a datestamp: :help abbreviations

:iab dts <C-R>=strftime("%c")<CR>

To replace text with the current date in a substitute command:

:s/text to replace/\=strftime("%c")/

Vary the format string (the "%c" argument), to change how the time and/or date are displayed. For some formats, the result may depend on your locale. :help :language

The specification for the format string itself depends on the implementation of strftime() on your platform. For details, Unix users may refer to the strftime(3) man page, by running 'man 3 strftime'.

Some strftime() format string examples

Format String              Example output
-------------              --------------
%c                         Thu 27 Sep 2007 07:37:42 AM EDT (depends on locale)
%a %d %b %Y                Thu 27 Sep 2007
%b %d, %Y                  Sep 27, 2007
%d/%m/%y %H:%M:%S          27/09/07 07:36:32
%H:%M:%S                   07:36:44
%T                         07:38:09
%m/%d/%y                   09/27/07
%y%m%d                     070927
%x %X (%Z)                 09/27/2007 08:00:59 AM (EDT)

RFC822 format:
%a, %d %b %Y %H:%M:%S %z   Wed, 29 Aug 2007 02:37:15 -0400

ISO8601/W3C format (http://www.w3.org/TR/NOTE-datetime):
%FT%T%z                    2007-08-29T02:37:13-0400

Using external tools

On Unix-based systems, enter the following in Vim to read the output from running the date utility, inserting the result after the current line:

:r !date

Under Windows, use:

:r !date /t

Automatically update timestamps

You might want to automatically update existing time stamps when writing a file.

This is a solution for html implemented as an autocmd which fires when the file is written:

:au BufWritePre *.html exe "norm mz"|exe '%s/\(<!-- DATE -->\).\{-}\d\d:\d\d:\d\d/\1'.strftime("%b %d, %Y %X")."/e"|norm `z

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 which won't appear in an HTML document).

This is a general solution:

" If buffer modified, update any 'Last modified: ' in the first 20 lines.
" 'Last modified: ' can have up to 10 characters before (they are retained).
" Restores position using s mark.
function! LastModified()
  if &modified
    normal ms
    let n = min([20, line("$")])
    exe '1,' . n . 's#^\(.\{,10}Last modified: \).*#\1' .
          \ strftime('%a %b %d, %Y  %I:%M%p') . '#e'
    normal `s
  endif
endfun
autocmd BufWritePre * call LastModified()

Updating a DNS SOA serial number

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):

:nnoremap <F8> /\<\d\{10}\><CR>ce<C-r>=strftime("%Y%m%d00")<CR><Esc>:echo @"<CR>

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).

See also

Comments

You could also use the following :execute commands:

nmap <Leader>tt :execute "normal i" . strftime("%x %X (%Z) ")<Esc>
imap <Leader>tt <Esc>:execute "normal i" . strftime("%x %X (%Z) ")<Esc>i

By default, <Leader> is "\". :help mapleader In my case I hit "\tt" in normal or insert mode and hello time and date.