2

Question

How does one effectively find today's date in a file?
The format of the date is given by GNU's date(1)'s command as:

$ date '+%a %d-%m-%Y'
Mon 16-08-2021

What I already tried

This is how I got this to work right now.

Step 1. I have defined a vimscript :Date command to output the wanted format:

function Date()
   read !date '+\%a \%d-\%m-\%Y'
   -1
   join
endfunction
command Date :call Date()

Step 2 I open up the command-line-window in search mode with q/.
Step 3 I run :Date to output today's date in the command-line-window.
Step 4 I press <Enter> to find the date.

Steps 2, 3, and 4 have to be repeated every time to find today's date.
So, oftentimes, I just search for the date manually (e.g. /Mon 16-08-2021).
How can I make this process more efficient?

3

This will search for today's date:

/<C-R>=strftime('%a %d-%m-%Y')<CR><CR>

You can define a mapping to create a shortcut, for example, to search with <Ctrl>G in normal mode you can use:

nnoremap <C-G> /<C-R>=strftime('%a %d-%m-%Y')<CR><CR>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.