commit 5173978531cede1aeb092dd36e833c043770a77f
parent ce7753e60ff29c31d213f1e127a3f859041c7b91
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/.vim/plugin/snippets.vim b/.vim/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