<CR>
, <Enter>
, and <C-M>
are all synonyms in Vim. Therefore, it's sufficient to define this mapping:
:imap <buffer> <CR> <CR><C-R>=strftime("%Y-%m-%d %a %I:%M %p ")<CR>
I chose a buffer-local mapping because you probably only want this for certain filetypes. You can define that for certain filetypes by prepending :autocmd Filetype <filetype> ...
, and put that into your ~/.vimrc
. But that get's unwieldy as you add mappings and other settings for various filetypes. Better put the commands into ~/.vim/ftplugin/<filetype>_mappings.vim
. (This requires that you have :filetype plugin on
.)
This still won't cover pasted text. I doubt you'd really want that (it's better to prepend the timestamps afterwards), but you can look into the (recently added) TextChanged
event to implement that.