randomfox (randomfox) wrote,

Vim: Date stamps

The first set of Vim mappings will insert the current date. The second set of Vim mappings will update a line of the form "Last updated: date" so that it holds the current date.

" Returns the current date in string form.
" The substitution gets rid of the leading 0 in the day of the month.
function! Date_string()
    return substitute(strftime("%B %d, %Y"), " 0", " ", "")

" F12 t inserts a date stamp.
" F12 t in visual mode replaces the selection with a date stamp.
nmap <F12>s i<C-R>=Date_string()<CR><ESC>
vmap <F12>s c<C-R>=Date_string()<CR><ESC>
imap <F12>s <C-R>=Date_string()<CR>

" F12 l updates a 'Last up-dated:' line
" Need to use a [s] here so that this macro won't change itself if
" invoked on this file.
let m = "1G/La[s]t updated:/e+0<CR>a <C-R>=Date_string()<CR><CR><ESC>dd"
execute "nmap <F12>l" m
execute "imap <F12>l <esc>" . m . "i"
execute "vmap <F12>l <esc>" . m
unlet m

  • Post a new comment


    Anonymous comments are disabled in this journal

    default userpic

    Your reply will be screened

    Your IP address will be recorded