Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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

:r!date
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
2012-07-20

In vim

:r!date "+%F"
message.to.followup.lstF

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.

share|improve this question
2  
wow, new trick I learned today :) ty –  Sandro Dzneladze Jul 20 '12 at 8:01
add comment

3 Answers

up vote 7 down vote accepted

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

<C-r>=strftime('%F')<CR>
share|improve this answer
    
I am choosing your answer as it the most vim like way to do things. –  nelaar Jul 23 '12 at 7:18
add comment

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"
2012-07-20

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

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

share|improve this answer
    
+1 That you can use with other programs than date too, and hence its easier to remember than the internat "strftime"-thing. –  math Jul 25 '12 at 7:59
add comment

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>
share|improve this answer
1  
<C-r> is very useful, I wish I had spent more time reading the help in vim. –  nelaar Jul 20 '12 at 8:41
    
It's never too late. –  romainl Jul 20 '12 at 10:22
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.