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

How do I paste the result of a calculation or expression in normal mode?

Answer

"=expression<CR>p

Explanation

The expression register ("=) lets you evaluate any Vimscript expression and paste the result directly into your buffer from normal mode. Press "=, type an expression, hit <CR>, then p or P to paste the result. This turns Vim into a calculator, date inserter, or dynamic text generator without leaving your editor.

How it works

  • "= tells Vim you want to use the expression register
  • Vim opens a prompt at the bottom of the screen where you type any valid Vimscript expression
  • Press <CR> to evaluate the expression — the result is now stored in the = register
  • Press p to paste after the cursor or P to paste before it

Example

Calculate a value and paste it:

"=128*1024<CR>p

This inserts 131072 at the cursor position.

Paste the current date:

"=strftime('%Y-%m-%d')<CR>p

Inserts something like 2025-01-15 at the cursor.

Generate a string:

"=repeat('-', 40)<CR>p

Inserts a line of 40 dashes: ----------------------------------------

Tips

  • Use "=system('whoami')<CR>p to paste the output of any shell command
  • Access Vim variables: "=&tabstop<CR>p pastes your current tabstop setting
  • Use "=line('.')<CR>p to paste the current line number
  • Use "=expand('%:t')<CR>p to paste the current filename without the path
  • The expression register works with P (paste before), "=expr<CR>P
  • Combine with ranges: in visual mode, select text and use c<C-r>=expression<CR> to replace the selection with a computed value
  • After evaluating once, "= remembers the last expression — press "=<CR>p (empty expression) to re-evaluate and paste it again

Next

How do I edit multiple lines at once using multiple cursors in Vim?