vimtricks.wiki Concise Vim tricks, one at a time.

How do I insert the current date or time into the buffer using Vim's built-in expression evaluation?

Answer

:put =strftime('%Y-%m-%d')

Explanation

The :put = command inserts the result of a Vimscript expression directly into the buffer as a new line. Combined with strftime(), it provides a quick way to stamp the current date or time into a file without leaving Vim or using a shell command.

How it works

  • :put ={expr} evaluates {expr} and appends the result on a new line below the cursor
  • strftime('{format}') returns the current date/time using C-style format codes

Common format codes:

Code Meaning Example
%Y 4-digit year 2026
%m 2-digit month 03
%d 2-digit day 04
%H:%M 24-hour time 14:30
%A Full weekday Wednesday
%b Short month Mar

Example

:put =strftime('%Y-%m-%d')
" Inserts on a new line: 2026-03-04

:put =strftime('%a %b %d, %Y')
" Inserts: Wed Mar 04, 2026

For inline insertion at the cursor position without creating a new line, use the expression register in insert mode:

" In insert mode:
<C-r>=strftime('%H:%M')<CR>

Tips

  • Map it for convenience: nnoremap <leader>dt :put =strftime('%Y-%m-%d')<CR>
  • Use :-1join after :put = to merge the inserted line with the one above if you need inline insertion from normal mode
  • strftime format strings are platform-dependent; on Windows %e (space-padded day) may not work — use %d instead

Next

How do I make the focused window automatically expand to a minimum size while keeping other windows visible?