tags:

views:

1934

answers:

7

EditPad Lite has a nice feature (CTRL-E, CTRL-I) which inserts a time stamp e.g. "2008-09-11 10:34:53" into your code.

What is the best way to get this functionality in Vim?

(I am using Vim 6.1 on a Linux server via SSH. In the current situation a number of us share a login so I don't want to create abbreviations in the home directory if there is another built-in way to get a timestamp.)

+7  A: 

http://kenno.wordpress.com/2006/08/03/vim-tip-insert-time-stamp/

Tried it out, it works on my mac:

:r! date

produces:

Thu Sep 11 10:47:30 CEST 2008

This:

:r! date "+%Y-%m-%d %H:%M:%S"

produces:

2008-09-11 10:50:56
Daren Thomas
+1  A: 

:r! date

You can then add format to the date command (man date) if you want the exact same format and add this as a vim alias as well

:r! date +"\%Y-\%m-\%d \%H:\%M:\%S"

That produces the format you showed in your example (date in the shell does not use \%, but just %, vim replaces % by the name of the current file, so you need to escape it).

You can add a map in your .vimrc for it to put the command automatically, for instance, each time you press F3:

:map :r! date +"\%Y-\%m-\%d \%H:\%M:\%S"

(Edited the from above :) )

Vinko Vrsalovic
+1  A: 

For a unix timestamp:

:r! date +\%s

You can also map this command to a key (for example F12) in VIM if you use it a lot:

Put this in your .vimrc:


map  <F12> :r! date +\%s<cr>
D4V360
+10  A: 

To make it work cross-platform, just put the following in your vimrc:

nmap <F3> a<C-R>=strftime("%Y-%m-%d %a %I:%M %p")<CR><Esc>
imap <F3> <C-R>=strftime("%Y-%m-%d %a %I:%M %p")<CR>

Now you can just press F3 any time inside Vi/Vim and you'll get the current timestamp inserted at the cursor.

Swaroop C H
+2  A: 

Have a look to the tip dedicated to time stamp insertion/update on vim.wikia.

Luc Hermitte
A: 

Timestamp script

br1
+1  A: 

As an extension to @Swaroop C H's answer,

^R=strftime("%FT%T%z")

is a more compact form that will also print the time zone (actually the difference from UTC, in an ISO-8601-compliant form).

If you prefer to use an external tool for some reason,

:r !date --rfc-3339=s

will give you a full RFC-3338 compliant timestamp; use ns instead of s for Spock-like precision.

Also you might find it useful to know that

:source somefile.vim

will read in commands from somefile.vim: this way you could set up a custom set of mappings, etc., and then load it when you're using vim on that account.

intuited