I am trying to save a file with today's date as part of name.

To save file, we can just do :w myFile. To get today's date, we can use strftime. Ex: :echo strftime("%F") " returns 2020-03-27.

Now I would like to combine both - to save with today's date. Something like:

:w 'myFile-(strftime("%F")'

Is it possible to save file with today's date (myFile-2020-03-27.) in Vim?


There are two ways to do this.

The first one is interactive, using CTRL-R = in the Ex command-line to enter an expression and evaluate it.

Start by typing the command as usual: :w myFile-, then press CTRL-R, =. You'll now have a = prompt where you can enter an expression. Enter strftime("%F") and press "Enter", Vim will evaluate that to 2020-03-27 (assuming that's today's date) and will now display the command-line as :w myFile-2020-03-27, which you can then execute or further edit as you like.

Another option is to use the :execute command to execute a string as an Ex command. In which case you can simply use:

:exe 'w myFile-'.strftime("%F")

Note that . is the string concatenation operator here. This latter form with :execute is particularly useful in Vim scripts or functions.


If you frequently find yourself wanting to insert the current date into command-line commands you could set up a mapping like this using something similar to what's in the existing answer:

:cnoremap YMD <C-R>=strftime("%Y-%m-%d")<CR>

Your use case then would be to enter

:w myFile-YMD

Which, right after you hit D, will be converted to

:w myFile-2020-03-27

and then you just hit Enter to submit it. I have this in my vimrc and it comes in handy.

  • I really like this idea in general. But I find that :cabbrev <expr> YMD strftime("%F") is a better way to implement it. Using a cabbrev means it won't make it awkward to type a single Y or YM on the command line. It will still properly expand if YMD appears after a -. Also using <expr> is much simpler than <C-R>= here, since it's possible to do so. One small downside of cabbrev is that it doesn't expand on D, so if you type :w myFile-YMD<CR> you won't "see" the date until the command is completed. You can work around that by expanding it explicitly with <C-]> though. – filbranden Mar 28 '20 at 17:52
  • That's not my experience at all. Zero awkwardness. Have you tried it with cnoremap? Putting aside the fact that "Y" or "YM" (and, for many people I'd wager, capital letters in general) are used relatively infrequently on the command line there's no delay if I type something other than "MD"/"D" after them. Vim only waits if you wait. And then it only waits for however long you've configured. I have 'ttimeout' enabled and 'ttimeoutlen' set to 100 (these are also Vim's recommended values as you can see in defaults.vim). There's also no problem using "YMD" after a -. (cont) – B Layer Mar 29 '20 at 3:16
  • And finally, I like the value to be inserted immediately...not uncommon for me to flip the date to yesterday or tomorrow, for example, and I'd rather not stretch for a <C-]> if I don't have to. – B Layer Mar 29 '20 at 3:16

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.