Vim Tips Wiki
(another big change: factored out a lot of unrelated info. Is it better? If not, revert please :))
m (typos)
Line 15: Line 15:
 
== Using strftime() ==
 
== Using strftime() ==
   
Vim's internal strftime() function ({{help|strftime()}}) returns a date/time string, given a format string, but this might not work on all systems. To retrieve the output of a Vim function for pasting, the <tt>"=</tt> register ({{help|id="=}}) is used. Here's a bunch of examples:
+
Vim's internal strftime() function ({{help|strftime()}}) returns a date/time string, given a format string, but this might not work on all systems. To store return value of this Vim function for pasting, the <tt>"=</tt> register ({{help|id="=}}) is used. Here's a bunch of examples:
   
 
In normal mode, this will paste the current datestamp (press the Enter key where you see <tt>&lt;Enter&gt;</tt>):
 
In normal mode, this will paste the current datestamp (press the Enter key where you see <tt>&lt;Enter&gt;</tt>):
Line 27: Line 27:
 
This makes <tt>dts</tt> typed in insert mode expand to a datestamp ({{help|abbreviations}}):
 
This makes <tt>dts</tt> typed in insert mode expand to a datestamp ({{help|abbreviations}}):
   
:iab hms &lt;C-R&gt;=strftime("%c")&lt;CR&gt;
+
:iab dts &lt;C-R&gt;=strftime("%c")&lt;CR&gt;
   
 
To embed the current date in a substitution:
 
To embed the current date in a substitution:
Line 41: Line 41:
 
Format String Example output
 
Format String Example output
 
------------- --------------
 
------------- --------------
"%c" Thu 27 Sep 2007 07:37:42 AM EDT (depends on locale)
+
%c Thu 27 Sep 2007 07:37:42 AM EDT (depends on locale)
"%a %d %b %Y" Thu 27 Sep 2007
+
%a %d %b %Y Thu 27 Sep 2007
"%b %d, %Y" Sep 27, 2007
+
%b %d, %Y Sep 27, 2007
"%d/%m/%y %H:%M:%S" 27/09/07 07:36:32
+
%d/%m/%y %H:%M:%S 27/09/07 07:36:32
"%H:%M:%S" 07:36:44
+
%H:%M:%S 07:36:44
"%T" 07:38:09
+
%T 07:38:09
"%m/%d/%y" 09/27/07
+
%m/%d/%y 09/27/07
"%y%m%d" 070927
+
%y%m%d 070927
   
 
RFC822 format:
 
RFC822 format:
"%a, %d %b %Y %H:%M:%S %z" Wed, 29 Aug 2007 02:37:15 -0400
+
%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):
 
ISO8601/W3C format (http://www.w3.org/TR/NOTE-datetime):
"%FT%T%z" 2007-08-29T02:37:13-0400
+
%FT%T%z 2007-08-29T02:37:13-0400
   
 
== Using external tools ==
 
== Using external tools ==

Revision as of 11:53, 27 September 2007

Previous TipNext Tip

Tip: #97 - Insert current date or time

Created: August 9, 2001 Complexity: basic Author: newbie Version: 6.0 Karma: 185/81 Imported from: Tip#97

There are a variety of ways to automatically add a date/time stamp to your editing buffer, similar to <F5> in Windows Notepad.

Using strftime()

Vim's internal strftime() function (:help strftime()) returns a date/time string, given a format string, but this might not work on all systems. To store return value of this Vim function for pasting, the "= register (:help "=) is used. Here's a bunch of examples:

In normal mode, this will paste the current datestamp (press the Enter key where you see <Enter>):

"=strftime("%c")<Enter>p

This makes <F5> in insert mode paste a datestamp (:help i_CTRL-R):

:imap <F5> <C-R>=strftime("%c")<CR>

This makes dts typed in insert mode expand to a datestamp (:help abbreviations):

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

To embed the current date in a substitution:

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

Vary the format string, i.e. the "%c" argument to strftime(), to get the time and/or date in a different format. Depending on the format string used, 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' at the prompt.

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

Unix users have access to the date utility. You may ask Vim to run 'date' and insert the output in the buffer:

:r!date

Under Windows,

:r!date /t

Alternatively, Windows users may install cygwin from http://sources.redhat.com/cygwin for access to Unix 'date' and a host of other utilities, but that might be overkill if you're just looking for datestamps.

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 <!-- DATE -->Aug 13, 2001 14:19:50 is embedded in the text, and it will be updated to the current date (using Vim's built-in strftime() function) and time automatically – every time I save the file (the ...DATE... stuff is an HTML comment which won't appear in an HTML document).

This is a general solution:

" Add a function that returns a time stamp in the desired format.
if !exists("*TimeStamp")
  fun TimeStamp()
    return "Time-stamp: <" . strftime("%d %b %Y %X") . " My Name>"
  endfun
endif

" Search for an existing time stamp and update it.
if !exists("*UpdateTimeStamp")
  fun UpdateTimeStamp()
    if (match(getline(1),"Time-stamp: <.*>")) > 1
      exe "1,1 s/Time-stamp: <.*>/" . TimeStamp()
    endif
  endfun
endif

" Abbreviation to manually enter a timestamp (type YTS in insert mode).
iab YTS <C-R>=TimeStamp()<CR>

" Add an autocommand to update an existing time stamp when writing the file.
" It uses the functions above to replace the time stamp and restores cursor
" position afterwards.
autocmd BufWritePre,FileWritePre * ks|call UpdateTimeStamp()|'s

The date is searched for in the first line of the file only (you can change that) in columns > 1. I did that mainly for security reasons (in source code, there will be almost always a comment start, e.g. /* in c, preceding the time stamp).

Comments

I mapped the time function in vimrc:

map T "=strftime("%m/%d/%y %H:%M")<CR>p

The leading quotation mark is necessary and there is no closing quote.


as well as these "= commands:

" in normal mode, paste before cursor position, allows date insertion
" at the start of the line.
nmap <leader>tt "=strftime("%x %X (%Z) ")<CR>P

" in insertmode, esc to normal, paste, and return to insert
imap <leader>tt <Esc>"=strftime("%x %X (%Z) ")<CR>Pi

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 "\", but it's a variable you can modify. In my case I hit "\tt" in normal or insert mode and hello time and date. See "man strftime" to find replacements for the date format "%x %X (%Z)".