How do I insert the result of a Vim expression onto a new line in the buffer?
Answer
:put ={expression}
Explanation
The :put command inserts the contents of a register as a new line below the cursor. When combined with the expression register =, it evaluates any Vim expression and places the result on its own line — without entering Insert mode or disturbing any named register.
How it works
:put =opens the expression register and prompts for an expression- After typing the expression and pressing
<CR>, Vim evaluates it and inserts the result as a new line below the cursor - The expression can be arithmetic, string functions, system calls, or any valid Vim expression
:put! =inserts above the current line instead of below- The cursor moves to the newly inserted line
Example
Insert today's date:
:put =strftime('%Y-%m-%d')
Insert a computed value:
:put =40 * 2 + 2
Result inserted as new line:
82
Insert a list of numbers from 1 to 5:
:for i in range(1,5) | put =i | endfor
Tips
- Compare with
<C-r>=in Insert mode: that inserts inline at the cursor position without a newline - Use
:put =execute('version')to dump multi-line command output into the buffer for easy inspection :put =@"inserts the contents of the unnamed register as a new line — useful when you want to paste register content as a line even if it was a character-wise yank