Vim Tips Wiki

Changes: Insert current date or time


Back to page

(Using external tools: Clarify.)
(Thank you for the auspicious writeup. It in fact was a amusement account it. Look advanced to far added agreeable from you! By the way, how could we communicate? efgfbabdafcggbga)
Line 1: Line 1:
Thank you for the auspicious writeup. It in fact was a amusement account it. Look advanced to far added agreeable from you! By the way, how could we communicate? efgfbabdafcggbga
|category1=Automated Text Insertion
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 <code>strftime()</code>, but some don't. To store the return value of the function, the <code>"=</code> register ({{help|id="=}}) 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 <code>P</code> 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 <code>dts</code> 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 <code>"%c"</code> 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 <code>strftime()</code> on your platform. For details, Unix users may refer to the <code>strftime(3)</code> man page, by running '<code>man 3 strftime</code>'.
===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 (
%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 <code>date</code> utility, inserting the result after the current line:
:r !date
Under Windows, use:
:r !date /t
The above commands insert the output of the <code>date</code> program after the current line. If wanted, the <code>!!</code> command can be used to filter the current line, replacing it with the output of <code>date</code>.
# Under Windows, use:
!!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 <code><!-- DATE -->Aug 13, 2001 14:19:50</code> 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).
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("$")])
keepjumps exe '1,' . n . 's#^\(.\{,10}Last modified: \).*#\1' .
\ strftime('%a %b %d, %Y %I:%M%p') . '#e'
call histdel('search', -1)
call setpos('.', save_cursor)
autocmd BufWritePre * call LastModified()
<code>keepjumps</code> excludes timestamp position from jump list, which is a false positive in this context. Calling <code>histdel</code> removes timestamp search pattern from search history.
==Updating timestamps in XML files==
An XML file may contain a line like the following (the value is a date/time given as the number of milliseconds past the [[wikipedia:Unix time|epoch]]):
<property name='p2.timestamp' value='1333222444000'/>
With the code below in your [[vimrc]], enter <code>:UpdateTimestamps</code> to update the value to the current time in each instance of a tag with the format shown above. The code uses Vim's <code>strftime()</code> which requires support by the operating system, so this will only work on systems which handle <code>%s</code> (the number of seconds since the start of 1970-01-01; should be available on Unix systems but not Windows).
function! s:UpdateTimestamps()
let tstamp = strftime('%s000')
%s#<property name='p2.timestamp' value='\zs\d\+\ze'/>#\=tstamp#g
echo 'New time: ' . tstamp
command! UpdateTimestamps call s:UpdateTimestamps()
It is not necessary to type the full command: just type <code>:Up</code> then press Tab for command completion. Using a command allows updating to be easily applied to [[Run a command in multiple buffers|multiple buffers]], for example, entering <code>:bufdo UpdateTimestamps</code> would update all tags in all buffers.
==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 <code>\<</code> (beginning word), followed by 10 digits, followed by <code>\></code> (end word). The <code>ce</code> changes to the end of the word (deleting the number to the unnamed register), then inserts (Ctrl-r) the value of register <code>=</code> (which evaluates the following expression).
==See also==
*[[VimTip1521|Automatically Update Copyright Notice in Files]]

Revision as of 05:31, June 5, 2014

Thank you for the auspicious writeup. It in fact was a amusement account it. Look advanced to far added agreeable from you! By the way, how could we communicate? efgfbabdafcggbga

Around Wikia's network

Random Wiki