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

<p>What is the best way to get this functionality in Vim?</p>

<p>(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.)</p>

[{'answer_id': 56061, 'author': 'Daren Thomas', 'author_id': 2260, 'author_profile': 'https://Stackoverflow.com/users/2260', 'pm_score': 8, 'selected': True, 'text': '<p><a href="http://kenno.wordpress.com/2006/08/03/vim-tip-insert-time-stamp/" rel="noreferrer">http://kenno.wordpress.com/2006/08/03/vim-tip-insert-time-stamp/</a></p>\n\n<p>Tried it out, it works on my mac:</p>\n\n<pre><code>:r! date\n</code></pre>\n\n<p>produces:</p>\n\n<pre><code>Thu Sep 11 10:47:30 CEST 2008\n</code></pre>\n\n<p>This:</p>\n\n<pre><code>:r! date "+\\%Y-\\%m-\\%d \\%H:\\%M:\\%S"\n</code></pre>\n\n<p>produces:</p>\n\n<pre><code>2008-09-11 10:50:56\n</code></pre>\n'}, {'answer_id': 56064, 'author': 'Vinko Vrsalovic', 'author_id': 5190, 'author_profile': 'https://Stackoverflow.com/users/5190', 'pm_score': 3, 'selected': False, 'text': '<p>:r! date </p>\n\n<p>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</p>\n\n<p>:r! date +"\\%Y-\\%m-\\%d \\%H:\\%M:\\%S"</p>\n\n<p>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).</p>\n\n<p>You can add a map in your .vimrc for it to put the command automatically, for instance, each time you press F3:</p>\n\n<pre><code>:map &lt;F3&gt; :r! date +"\\%Y-\\%m-\\%d \\%H:\\%M:\\%S"&lt;cr&gt;\n</code></pre>\n\n<p>(Edited the  from above :) )\n(Edit: change text part to code, so that </p>\n\n<pre><code>&lt;F3&gt; \n</code></pre>\n\n<p>can be displayed)</p>\n'}, {'answer_id': 56069, 'author': 'fijter', 'author_id': 3215, 'author_profile': 'https://Stackoverflow.com/users/3215', 'pm_score': 2, 'selected': False, 'text': '<p>For a unix timestamp:</p>\n\n<pre>\n:r! date +\\%s\n</pre>\n\n<p>You can also map this command to a key (for example F12) in VIM if you use it a lot:</p>\n\n<p>Put this in your .vimrc:</p>\n\n<pre><code>\nmap  &lt;F12&gt; :r! date +\\%s&lt;cr&gt;\n</code></pre>\n'}, {'answer_id': 58604, 'author': 'Swaroop C H', 'author_id': 4869, 'author_profile': 'https://Stackoverflow.com/users/4869', 'pm_score': 7, 'selected': False, 'text': '<p>To make it work cross-platform, just put the following in your <code>vimrc</code>:</p>\n\n<pre><code>nmap &lt;F3&gt; i&lt;C-R&gt;=strftime("%Y-%m-%d %a %I:%M %p")&lt;CR&gt;&lt;Esc&gt;\nimap &lt;F3&gt; &lt;C-R&gt;=strftime("%Y-%m-%d %a %I:%M %p")&lt;CR&gt;\n</code></pre>\n\n<p>Now you can just press F3 any time inside Vi/Vim and you\'ll get a timestamp like <code>2016-01-25 Mo 12:44</code> inserted at the cursor.</p>\n\n<p>For a complete description of the available parameters check the <a href="http://www.cplusplus.com/reference/ctime/strftime/" rel="noreferrer">documentation of the C function strftime()</a>.</p>\n'}, {'answer_id': 2979150, 'author': 'intuited', 'author_id': 192812, 'author_profile': 'https://Stackoverflow.com/users/192812', 'pm_score': 3, 'selected': False, 'text': '<p>As an extension to @Swaroop C H\'s answer,</p>\n\n<pre><code>^R=strftime("%FT%T%z")\n</code></pre>\n\n<p>is a more compact form that will also print the time zone (actually the difference from UTC, in an ISO-8601-compliant form).</p>\n\n<p>If you prefer to use an external tool for some reason,</p>\n\n<pre><code>:r !date --rfc-3339=s\n</code></pre>\n\n<p>will give you a full RFC-3339 compliant timestamp; use <code>ns</code> instead of <code>s</code> for Spock-like precision, and pipe through <code>tr \' \' T</code> to use a capital T instead of a space between date and time.</p>\n\n<p>Also you might find it useful to know that</p>\n\n<pre><code>:source somefile.vim\n</code></pre>\n\n<p>will read in commands from <code>somefile.vim</code>: this way you could set up a custom set of mappings, etc., and then load it when you\'re using vim on that account.</p>\n'}, {'answer_id': 7681121, 'author': 'luser droog', 'author_id': 733077, 'author_profile': 'https://Stackoverflow.com/users/733077', 'pm_score': 4, 'selected': False, 'text': '<p>Why is everybody using <code>:r!</code>? Find a blank line and type <code>!!date</code> from command-mode. Save a keystroke!</p>\n\n<p>[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.]</p>\n'}, {'answer_id': 11325690, 'author': 'lukmac', 'author_id': 555951, 'author_profile': 'https://Stackoverflow.com/users/555951', 'pm_score': 0, 'selected': False, 'text': '<p>Another quick way not included by previous answers: type-</p>\n\n<p>!!date</p>\n'}, {'answer_id': 22578234, 'author': 'user3449771', 'author_id': 3449771, 'author_profile': 'https://Stackoverflow.com/users/3449771', 'pm_score': 3, 'selected': False, 'text': '<p>From the <a href="http://vim.wikia.com/wiki/Insert_current_date_or_time" rel="noreferrer">Vim Wikia</a>.</p>\n\n<p>I use this instead of having to move my hand to hit an F key:</p>\n\n<pre><code>:iab &lt;expr&gt; tds strftime("%F %b %T")\n</code></pre>\n\n<p>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.</p>\n\n<p>I put the <code>%b</code> in there, because I like seeing the month name.  The <code>%F</code> gives me something to sort by date.  I might change that to <code>%Y%m%d</code> so there are no characters between the units.</p>\n'}, {'answer_id': 24840035, 'author': 'byte-pirate', 'author_id': 1637043, 'author_profile': 'https://Stackoverflow.com/users/1637043', 'pm_score': 3, 'selected': False, 'text': '<h3>Unix,use:</h3>\n<pre><code>!!date\n</code></pre>\n<h3>Windows, use:</h3>\n<pre><code>!!date /t\n</code></pre>\n<h3>More details:see <a href="http://vim.wikia.com/wiki/Insert_current_date_or_time" rel="nofollow noreferrer">Insert_current_date_or_time</a></h3>\n'}, {'answer_id': 57789091, 'author': 'luator', 'author_id': 2095383, 'author_profile': 'https://Stackoverflow.com/users/2095383', 'pm_score': 2, 'selected': False, 'text': '<p>I wanted a custom command <code>:Date</code> (not a key mapping) to insert the date at the current cursor position.</p>\n\n<p>Unfortunately straightforward commands like <code>r!date</code> result in a new line.  So finally I came up with the following:</p>\n\n<pre><code>command Date execute "normal i&lt;C-R&gt;=strftime(\'%F %T\')&lt;CR&gt;&lt;ESC&gt;"\n</code></pre>\n\n<p>which adds the date/time string at the cursor position without adding any new line (change to <code>normal a</code> add after the cursor position).</p>\n'}, {'answer_id': 66021073, 'author': 'mmrtnt', 'author_id': 9302794, 'author_profile': 'https://Stackoverflow.com/users/9302794', 'pm_score': 1, 'selected': False, 'text': '<p>I\'m using vi in an Eterm for reasons and it turns out that strftime() is not available in vi.</p>\n<p>Fought long and hard and finally came up with this:</p>\n<pre><code>map T :r! date +&quot;\\%m/\\%d/\\%Y \\%H:\\%M&quot; &lt;CR&gt;&quot;kkddo&lt;CR&gt;\n</code></pre>\n<p>Result: 02/02/2021 16:45</p>\n<p>For some reason, adding the date-time alone resulted in a blank line <em>above</em> the date-time and the cursor set <em>on</em> the date-time line.</p>\n<div class="s-table-container">\n<table class="s-table">\n<thead>\n<tr>\n<th></th>\n<th></th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td>date +&quot;[etc]&quot; &lt;CR&gt;</td>\n<td>Enters the date-time</td>\n</tr>\n<tr>\n<td>&quot;kk</td>\n<td>Moves up two lines</td>\n</tr>\n<tr>\n<td>dd</td>\n<td>Deletes the line above the date-time</td>\n</tr>\n<tr>\n<td>o &lt;CR&gt;</td>\n<td>Opens a line below the time and adds a carriage return (linefeed)</td>\n</tr>\n</tbody>\n</table>\n</div>\n<p><strong>Bonus:</strong></p>\n<p>vi doesn\'t read ~/.vimrc, it reads ~/.exrc</p>\n<p>Also, this is how it looks in vim/.vimrc:</p>\n<pre><code>map T  &quot;=strftime(&quot;%m/%d/%y %H:%M&quot;)&lt;CR&gt;po&lt;CR&gt;\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/56052', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/4639/']