Last week I wrote about Vim’s registers, and this week you’ll see one of the many reasons why they’re essential, in the form of
Inserting Registers in Insert Mode
When writing in insert mode, it’s possible to paste a register at the current location without leaving insert mode. To do this, press
CTRL-R then type the name of a register. For example,
CTRL-R a will insert the contents of
The text is inserted as if it were typed: that means
autoindent will be respected. Pressing
CTRL-R twice will insert the text literally.
CTRL-R = allows an expression to be entered -- press return to insert the result. This could be any expression, but an easy to learn trick is to use it for simple mathematics.
Let’s say I wanted to insert the number of milliseconds in a day. Rather than leaving Vim and using a calculator or REPL, I could type
CTRL-R =1000 * 60 * 60 * 24 and "86400000" will be inserted at the current position.
CTRL-R in command mode (not normal mode) will also insert the contents of a register.
For more on using
CTRL-R in insert mode, see
:help i_CTRL-R. The command mode equivalent is also documented in