In vim you can execute comands with "!". You can combine that with "r" to insert the output into your current buffer.

Fri Jul 20 09:39:26 SAST 2012

will insert the date into a file.

Now when I try to do some more interesting stuff like date with different format +%F. On the command line

$ date +%F

In vim

:r!date "+%F"

Which out puts the name of the file and puts F after it. some how the r!date "+%F" is being expanded in vim and not run on the command line. What do I need to do to run that so it puts the contents in vim.

Maybe vim has a better way to insert dates into files.


3 Answers 3


Vim has an internal strftime() function. Try this (in insert mode):

  • 2
    I am choosing your answer as it the most vim like way to do things.
    – nelaaro
    Commented Jul 23, 2012 at 7:18
  • 3
    And in normal mode this is the same (insert date at current position): "=strftime("%F")<CR>P (Source: vim.wikia.com/wiki/Insert_current_date_or_time)
    – erik
    Commented Jun 25, 2015 at 11:54

I kept experimenting till I figured out that vim was expanding the "%" character. So just escape "\%" and every thing works as I expected.

:r!date "+\%F"

Now I can put dates into files Like I would like to

:r!date "+\%F" -d "-2 day"

  • 1
    +1 That you can use with other programs than date too, and hence its easier to remember than the internat "strftime"-thing.
    – math
    Commented Jul 25, 2012 at 7:59

Another method, without escaping, using system():

system('date +%F')

In INSERT mode:

<C-r>=system('date +%F')<CR>

In NORMAL mode:

:put=system('date +%F')<CR>
  • 2
    <C-r> is very useful, I wish I had spent more time reading the help in vim.
    – nelaaro
    Commented Jul 20, 2012 at 8:41
  • 3
    It's never too late.
    – romainl
    Commented Jul 20, 2012 at 10:22

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .