Knowledge Base

Preserving for the future: Shell scripts, AoC, and more

Vimrc: adding timestamp functions

Just like my last post about an F5 timestamp function in scite, I wanted to apply the same functionality to Vim.

A friend of mine recently asked me, "What do you program in?" Turns out he meant IDE, and I said vim. I hardly ever work with compiled languages, so vim works great for me.

When first started in the Unix world, I swore to myself that I wouldn't work on a .vimrc because then I'd have to copy it everywhere, and sometimes I just don't have time or access to fling my ~/.vimrc file everywhere. But like an experienced Linux user, now I find myself building a larger and larger vimrc file.

Here's my timestamp functions for F5.

function! DateStamp()
   let a = strftime("%Y-%m-%d-") | let b = strftime("%w")+1 | let c = strftime(" %H:%M")
   execute "normal! i" . a . b . c
endfun
function! DateStampIsoUtc()
   let a = system("date -u +%FT%TZ")
   execute "normal! i" . a
endfun
" This is superfluous, but demonstrates command!
command! InsertDateStamp call DateStamp()
nmap <F5> i<C-O>:call DateStamp()<CR><Esc>:normal ll<CR><Esc>:<Esc>
imap <F5> <C-O>:call DateStamp()<CR><Esc>:normal l<CR>a
nmap <S-F5> i<C-O>:call DateStampIsoUtc()<CR><Esc>:normal ll<CR><Esc>:<Esc>
imap <S-F5> <C-O>:call DateStampIsoUtc()<CR><Esc>:normal l<CR>a
" Iso8601 but without timezone
nmap <C-F5> i<C-R>=strftime("%Y-%m-%dT%T")<CR><Esc>
imap <C-F5> <C-R>=strftime("%Y-%m-%dT%T")<CR>

Again, because I use a non-glibc-standard day of week numeral, my task is slightly longer than normal. But now I can press F5 in command or insert mode, and get a timestamp at the cursor!

Comments