sponsor Vim development Vim logo Vim Book Ad

basic Tip #97: How do I add a current time string inside Vim?

 tip karma   Rating 234/109, Viewed by 7985 

Read and edit this tip on the Vim tip wiki. The wiki may have a more recent version of this tip.

created:   August 9, 2001 1:06      complexity:   basic
author:   newbie      as of Vim:   6.0

This is a *request* for a tip.
Sometimes (eg. editing HTML pages) I need to add a timestamp string to my editing buffer.
On UNIX systems, I can use  
  :r!date  
to get a localized date time string; but on Windows ('date' on Windows will query the user to input new date)
or other platforms which does not have 'date' command, how do I get a timestamp easily?

 rate this tip  Life Changing Helpful Unfulfilling 

<< Cooperation of Gvim and AutoCad [MTEXT] | Getting vim help from mailing lists and newsgroups. >>

Additional Notes

none, August 9, 2001 4:27
The best thing to do is install cygwin from http://sources.redhat.com/cygwin.

This will give you 'date' and a host of other UNIX utilities under windows.
[email protected], August 9, 2001 5:54
vim has a function to help with this called strftime (do :help on it); these abbreviations work, but they aren't my own (i fear i forget whose file i got these from):

"date/time abbreviations
iab  mdyl  <c-r>=strftime("%a %d %b %Y")<cr>
iab  mdys  <c-r>=strftime("%y%m%d")<cr>
iab  mdyc  <c-r>=strftime("%c")<cr>
iab  hml   <c-r>=strftime("%d/%m/%y %H:%M:%S")<cr>
iab  hms   <c-r>=strftime("%H:%M:%S")<cr>
[email protected], August 9, 2001 9:03
You might want to automatically update existing time stamps when writing a file:

That's what's in my _vimrc:


--- CUT HERE ---
" first 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

" this function searches for an existing time stamp and updates it using the
" function declared above
   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. Just 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 (this is from the FAQ)
   autocmd BufWritePre,FileWritePre *   ks|call UpdateTimeStamp()|'s

--- CHAINSAW OFF ---

My time stamp has the format

Time-stamp: <09 Aug 2001 02:32:42 Editors Name>

It is searched for in the first line of the file only (you can certainly 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).

Kind regards

Sven
[email protected], August 14, 2001 8:12
I use the following insert-maps in my <.vimrc>:

" Dates/Times
imap \ymd   <C-R>=strftime("%y%m%d")<CR>
imap \mdy   <C-R>=strftime("%m/%d/%y")<CR>
imap \Mdy   <C-R>=strftime("%b %d, %Y")<CR>
imap \hms   <C-R>=strftime("%T")<CR>

So, while in insert mode, a \ymd will insert the year, month, and day in
the form 010814.

For html I have an autocmd that I use which fires on write:

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
embedded in the text which 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).


  
[email protected], January 1, 2002 10:43
try:
r!date /T
[email protected], February 1, 2004 16:45
to add the current date and time at the cursor (on any platform):

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

Vary the format string, i.e. the 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.

see
    :help quote=
    :help strftime()
    :help :language
[email protected], October 13, 2004 16:46
I want to embed the current date in a substitution:

:s/^abc/\/<today's date>/

Can any one please help?

Regards.

Anonymous, November 10, 2004 13:31
s/^abc/\=strftime("%c")/ should do it
[email protected], August 10, 2005 15:06
Building off Antoine's note, I mapped the time function in vimrc:

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

The leading quotation mark is necessary and solitary, i.e., no closing quotes.   I don't know about anyone else, but I had a heck of a time until I figured out that quote= literally meant "=
MjM
[email protected], August 20, 2005 13:08
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

<leader> by default 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)".
[email protected], September 29, 2006 9:57
How about recreating the F5 timestamp feature found in Notepad?
tarun<AT>tarunsadhana.com, October 25, 2006 0:55
for F5 mapping do the following :

imap <f5>   <C-R>=strftime("%d/%m/%Y %H:%M:%S")<CR>
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.
   
SourceForge.net Logo