Vim Recipes ‣ Typing ‣ Inserting the Date
You're typing a document and want to insert the current date.
For example, you're composing a letter and want to include today's date at the top.
Use !!date in Normal mode.
The !!command syntax executes command and replaces the current line with the output.This is in fact an oversimplification. count!!command actually filters count lines through command and returns the result, but when count is omitted, it has the effect I've described. This is why the text on the current line is replaced with the date.. To control where the date is inserted see the following paragraphs.
A similar same effect can be achieved with :r!date. The advantage is that it inserts the output of date on the next line, rather than replacing the current one.
The solutions above assume that you have a date executable in your PATH, as all UNIX-based systems should. I understand that Windows users have to use !!date /t.
Alternatively, you can call Vim's strftime() function, e.g. strftime("%c"). This will work on all systems which support the strftime system call, which I believe is the majority. You can map this function to a function key. For example, the following commands map <F7> to insert a date stamp after the cursor.
:inoremap <F7> <C-R>=strftime("%c")<CR>
:nnoremap <F7> "=strftime("%c")<CR>p
I suggest the use of !!date because it doesn't waste a function key, and, personally, I find it more memorable. If you intend to insert dates regularly, by all means remap a key to do so.