Vim Tips Wiki
Line 111: Line 111:
 
==See also==
 
==See also==
 
*[[VimTip1521|Automatically Update Copyright Notice in Files]]
 
*[[VimTip1521|Automatically Update Copyright Notice in Files]]
* [[http://www.makemoneyonline-money.com | Online Money Making]]
+
* [http://www.google-directions.com Google Biking Directions]
* [[Google all type of directions|http://www.google-directions.com]]
+
* [http://www.krugerrand-coins.com Krugerrand coins]
 
* [http://www.mapquestdrivingdirections.net Mapquest Maps Directions]
* [[Krugerrand Gold coins|http://www.krugerrand-coins.com]]
 
  +
* [http://www.smirlo.com Ganar Dinero Online]
* [[Mapquest Maps Directions|http://www.mapquestdrivingdirections.net]]
 
* [[Ganar Dinero Online|http://www.smirlo.com]]
+
* [http://www.peliculas-online-gratis.com Peliculas]
* [[Peliculas Gratis Online|http://www.peliculas-online-gratis.com]]
+
* [http://www.traductor-google.com Traductor de Google]
* [[Traductores de Google|http://www.traductor-google.com]]
+
* [http://www.googleearthgratis.com Google Earth]
* [[Google Earth|http://www.googleearthgratis.com]]
+
* [http://www.financial-planning-guide.net Financial Planning]
* [[Financial Planning|http://www.financial-planning-guide.net]]
+
* [http://www.0-credit-cards.net 0 Credit Cards]
  +
* [http://www.debtconsolidationloanstoday.com Debt Consolidation Loans]
* [[Credit Cards|http://www.0-credit-cards.net]]
 
* [[Loans|http://www.debtconsolidationloanstoday.com]]
+
* [http://www.onlinetips4u.com Money tips]
* [[Money tips|http://www.onlinetips4u.com]]
+
* [http://www.makemoneyworldwideinfo.com Make Money]
* [[Make Money|http://www.makemoneyworldwideinfo.com]]
+
* [http://www.ganadinero.co.uk Ganar Dinero]
 
* [http://descargarmessengergratis.blogspot.com Descargar Messenger Gratis]
* [[Hacer Dinero|http://www.ganadinero.co.uk]]
 
 
* [http://www.round-kitchen-table.com round kitchen table]
* [[Descargar Messenger Gratis|http://descargarmessengergratis.blogspot.com]]
 
 
* [http://makemoneyonlinesavvy.blogspot.com make money online]
* [[round kitchen table|http://www.round-kitchen-table.com]]
 
  +
* [http://www.how-to-make-money-fast.org fast money]
* [[make money online|http://makemoneyonlinesavvy.blogspot.com]]
 
* [[fast money|http://www.how-to-make-money-fast.org]]
+
* [http://www.how-to-make-a-website-hosting.com make a website]
  +
* [http://www.makemoneyonline-money.com Online Money Making]
   
 
==Comments==
 
==Comments==

Revision as of 21:27, 17 March 2010

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 using an expression :help :map-expression

:iab <expr> dts strftime("%c")

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 cursor and window position using save_cursor variable.
function! LastModified()
  if &modified
    let save_cursor = getpos(".")
    let n = min([20, line("$")])
    exe '1,' . n . 's#^\(.\{,10}Last modified: \).*#\1' .
          \ strftime('%a %b %d, %Y  %I:%M%p') . '#e'
    call setpos('.', save_cursor)
  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