Best way to insert timestamp in Vim?

Best way to insert timestamp in Vim?

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

Tabs and spaces in vim


Counting occurrences in Vim without marking the buffer changed

What is the most convincing command in Vim


To make it work cross-platform, just put the following in your vimrc:.
nmap <F3> i<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 a timestamp like 2016-01-25 Mo 12:44 inserted at the cursor.. For a complete description of the available parameters check the documentation of the C function strftime()..


As an extension to @Swaroop C H's answer,.
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-3339 compliant timestamp; use ns instead of s for Spock-like precision, and pipe through tr ' ' T to use a capital T instead of a space between date and time.. 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..


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


Why is everybody using :r!? Find a blank line and type !!date from command-mode.

Save a keystroke!. [n.b.

This will pipe the current line into stdin, and then replace the line with the command output; hence the "find a blank line" part.].


: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 <F3> :r! date +"\%Y-\%m-\%d \%H:\%M:\%S"<cr> 
(Edited the from above :) ) (Edit: change text part to code, so that .
can be displayed).


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> 



Another quick way not included by previous answers: type-. !!date.


From the Vim Wikia.. I use this instead of having to move my hand to hit an F key:.
:iab <expr> tds strftime("%F %b %T") 
Now in Insert mode it just type tds and as soon as I hit the space bar or return, I get the date and keep typing.. I put the %b in there, because I like seeing the month name.

The %F gives me something to sort by date.

I might change that to %Y%m%d so there are no characters between the units..




Windows, use:

!!date /t 

More details:see Insert_current_date_or_time

94 out of 100 based on 84 user ratings 334 reviews