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

How do I insert the result of a Vim expression or calculation directly into text?

Answer

"=

Explanation

The expression register ("=) lets you evaluate any Vim expression and insert its result as text. In insert mode, press <C-r>= to open a prompt at the bottom of the screen, type a Vim expression, press <CR>, and the result is inserted at the cursor. In normal mode, use "= before a put command (p or P) to paste the result.

How it works

  • <C-r>= — opens the expression prompt from insert mode
  • "= then p — evaluates the expression and pastes from normal mode
  • The expression can be any valid Vimscript: arithmetic, function calls, variable lookups, string operations

Example

Insert the current date while in insert mode:

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

Result inserted at cursor:

2024-03-15

Insert the result of a quick calculation:

<C-r>=1920 * 1080<CR>

Inserts 2073600 at the cursor.

Read an environment variable into the buffer:

<C-r>=expand('$HOME')<CR>

Tips

  • Use strlen(), substitute(), system(), or any built-in Vim function
  • system('date') runs a shell command and inserts its stdout — useful when strftime isn't enough
  • The expression register works wherever registers are accepted: :put =, <C-r>= in command-line mode, and i_CTRL-R_= in insert mode
  • Combine with @= to execute the expression register as a macro

Next

How do I run text I've yanked or typed as a Vim macro without recording it first?