commit b72ba8e04d0239ad6f92eefe0326d54061eff40e
parent 0fa603daea8f27a35b274ccd1944ad4e6d1baf3b
Author: Chris Bracken <chris@bracken.jp>
Date: Tue, 15 Feb 2022 17:25:47 -0800
Add CurrentDate function
When we're in a file with a filename whose root (prior to the extension)
is in YYYY-mm-dd form, return that date. Otherwise return the current
date.
Updates the <leader>cd mapping to insert this text instead of the
current date.
This is useful for vimwiki journal (diary) entry headings.
Diffstat:
1 file changed, 16 insertions(+), 1 deletion(-)
diff --git a/plugin/snippets.vim b/plugin/snippets.vim
@@ -1,7 +1,22 @@
" Code snippets
" Insert the current date in '2020-04-22 (Thu)' format.
-nnoremap <Leader>cd i<C-R>=strftime('%Y-%m-%d (%a)')<CR><Esc>
+nnoremap <Leader>cd i<C-R>=CurrentDate()<CR><Esc>
+
+" Returns the current date in YYYY-mm-dd form.
+" When in a file whose name is of the form YYYY-mm-dd, use that date instead.
+" This is useful for headings in VimWiki entries.
+function! CurrentDate()
+ " Get the filename without path or extension.
+ let fname = expand('%:t:r')
+ if fname =~ '\d\d\d\d-\d\d-\d\d'
+ let fdate = strptime('%Y-%m-%d', fname)
+ return strftime('%Y-%m-%d (%a)', fdate)
+ else
+ return strftime('%Y-%m-%d (%a)')
+ endif
+endfunc
+
" Inject a boilerplate C/C++/Obj-C main() function.
au FileType c,cpp,objc nmap <leader>cm iint main(int argc, char** argv) {<CR>return 0;<CR>}<ESC>%0