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

How do I insert today's date into a Vim buffer using the expression register?

Answer

<C-r>=strftime('%Y-%m-%d')<CR>

Explanation

The expression register ("=) lets you evaluate any Vimscript expression and insert its result inline. Combining it with strftime() gives you a fast, always-accurate way to stamp the current date anywhere in your buffer — without leaving insert mode or running a shell command.

How it works

  • <C-r> in insert mode opens the expression register prompt
  • = tells Vim you're providing an expression (not a register name)
  • strftime('%Y-%m-%d') calls Vim's built-in date-formatting function; it accepts any strftime(3) format string
  • <CR> evaluates the expression and inserts the result at the cursor

Example

With today being 2026-03-03, typing <C-r>=strftime('%Y-%m-%d')<CR> in insert mode inserts:

2026-03-03

You can adapt the format string for other styles:

<C-r>=strftime('%d %b %Y')<CR>   " → 03 Mar 2026
<C-r>=strftime('%Y%m%d')<CR>     " → 20260303
<C-r>=strftime('%H:%M')<CR>      " → 14:07  (current time)

Tips

  • Map it for quick access: inoremap <leader>dt <C-r>=strftime('%Y-%m-%d')<CR>
  • You can use strftime() from normal mode too: :put =strftime('%Y-%m-%d') inserts the date on a new line below the cursor
  • strftime() is built into Vim on all platforms — no plugins or external tools required

Next

How do I see exactly which highlight groups are responsible for the color of the character under the cursor in Neovim?