Evaluate an expression in any mode

From Vim Tips Wiki

Jump to: navigation, search
Tip 524 Printable Monobook Previous Next

created August 3, 2003 · complexity basic · author Yakov Lerner · version 6.0


The <CTRL-R>= things evaluates the expression. For example:

<CTRL-R>=12+34<CR>

<CTRL-R>= works in insert mode and in command mode. I felt it more convenient if <CTRL-R>= did the same thing in normal mode too.

I have this in my vimrc:

map <CTRL-R>= :echo

Now <CTRL-R>=123*456<CR> evaluates expressions for me in all 3 modes: normal mode, insert mode, and command mode. In fact I found it much more convenient to do casual calculations using <CTRl-R>= in normal mode than in any other mode.

Be warned: when typing <CTRL-R>= in normal mode, don't make long pause before '='. <CTRL-R> alone means 'redo' in normal mode. Vim is very smart, and figures the difference between single <CTRL-R> (which is 'redo') and the mapped sequence <CTRL-R>= (which is remapped). The 'timeoutlen' option controls this difference. The default value of 'timeoutlen' option is very convenient.

This is an expansion of Using Vim as calculator.

[edit] References

[edit] Comments

To get the behavior like i_CTRL-r in normal mode, use @=. For example, in normal mode, @='dd'<CR> will delete the current line just as if you had pressed dd in normal mode.

For more information. You can also do things like

:let @a = 'dd'

and then press @a in normal mode. This does the same thing.


Personal tools