How do I evaluate expressions and insert results inline in Vim?
Answer
<C-r>=system("date")<CR>
Explanation
The expression register (=) is one of Vim's most powerful yet underused features. While in insert mode, pressing <C-r>= opens a prompt where you can type any Vimscript expression. The result is inserted at the cursor position. This effectively turns Vim into a programmable text insertion engine.
How it works
<C-r>— In insert mode, triggers "insert from register."=— Selects the expression register, opening the=prompt at the bottom of the screen.- You type any valid Vimscript expression and press
<CR>to evaluate it. - The result is inserted as text at the cursor.
Example
Insert today's date while typing:
Before: Report created on |
(cursor at |, press <C-r>=system("date")<CR>)
After: Report created on Thu Feb 20 10:30:00 UTC 2026
More useful expressions:
" Insert current line number
<C-r>=line(".")<CR>
" Inline math
<C-r>=2048/16<CR> " inserts: 128
" Insert current filename
<C-r>=expand("%:t")<CR>
" Generate a numbered list item
<C-r>=line(".").'. '<CR>
Tips
- The expression register also works in command-line mode:
:<C-r>=lets you compute values for Ex commands. - Use
strftime("%Y-%m-%d")for formatted dates without relying on external commands. - You can reference any Vim variable:
<C-r>=g:my_var<CR>inserts its value. - Combine with macros: record a macro that uses
<C-r>=line('.')to insert incrementing numbers.