6

I saw someone insert the result of an equation in vim, but I don't remember how it was done.

For example say I want to insert the result of 7*7?

How do I insert the results of an equation?

9

For this to work, you need to use the expression register. Which allows you to execute arbitrary vimscript and do something about the result of the evaluation. To use the expression register on insert mode you type <C-r>= and then you can write vimscript on the command line to be executed. For your specific example you'll be doing the following keystrokes on insert mode:

<C-r>=7*7<CR>

So you'll end up with 49 inserted on your buffer.

  • What is <CR>? – Philip Kirkbride May 24 '17 at 16:13
  • 1
    It means Enter or Return, CR is kind of short for carriage return. It's very common to see it on vimscript and vimrc files. Try to search for it on vim help like: :h <CR> – rbernabe May 24 '17 at 16:24
3

Sorry to answer my own question so quickly. I found the following works in insert mode:

CRTL+r=7*7
ENTER
  • Ok, it's fine. You wer usint the expression register :) – rbernabe May 24 '17 at 16:12
0

I also encountered this issue and I ended up writing a plugin. It's called vim-calc. I hope people who have similar needs of a calculator in Vim will check it out!

Side-note: You can also use python to calculate, but has some limitations.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.