36

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

For example, say I want to calculate and insert the result of 7 * 7, is there a calculator that I can use to insert the results of this expression in Vim?

4 Answers 4

38

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.

5
  • What is <CR>? May 24, 2017 at 16:13
  • 4
    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, 2017 at 16:24
  • It's a very interesting solution. I wonder if it would be possible to get decimals.
    – Unix
    Sep 29, 2022 at 12:19
  • @PhilipKirkbride <C-r> means holding down "Control" and pressing "R" on the keyboard. <CR> means perssing "Enter".
    – Flimm
    Jun 5, 2023 at 11:40
  • If you're doing devision, one gotcha is that control-R followed by typing =3/2 prints out 1, not 1.5 . This is because Vim will return an integer if all the numbers preceding it are integers. To make Vim return a decimal number, you need to include the decimal point in the input, like this: press control-R, and type =3.0/2.0
    – Flimm
    Jun 5, 2023 at 11:42
10

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

Ctrl+r, then type =7*7, then press Enter

4
  • Ok, it's fine. You wer usint the expression register :)
    – rbernabe
    May 24, 2017 at 16:12
  • I much prefer the way this answer is written. You can also use the clipboard by issuing Ctrl-r " after =. It works well to use C to delete the expression into clipboard, enter insert mode, and "replace" it with the answer. Source.
    – mcp
    Apr 6, 2023 at 2:45
  • I prefer something like S, but it also yanks the newline character, which you then need to delete.
    – mcp
    Apr 6, 2023 at 2:58
  • If you're doing devision, one gotcha is that control-R followed by typing =3/2 prints out 1, not 1.5 . This is because Vim will return an integer if all the numbers preceding it are integers. To make Vim return a decimal number, you need to include the decimal point in the input, like this: press control-R, and type =3.0/2.0
    – Flimm
    Jun 5, 2023 at 11:42
3

If you wish to perform more advanced calculations than what is available via the expression register (e.g. including functions such as mean, sqrt, etc), you can use an external command line tool and pipe the current line to it from vim. I have found the tool calc to be useful here and it is available for several package managers (e.g apt install calc, but you could use any tool as long as it outputs easily to stdout).

So if we have a line that reads:

3 + 4

We can place our cursor on it in normal mode and press !!calc -p and hit Enter. You will see that it inserts the correct answer:

7

And that's it!


For a fancier solution, we could

  • Insert the original expression at the beginning of the line followed by an equal sign.
  • Make it work on lines that already have an = in them (i.e. updating a calculation).
  • Round answers to a limited number of decimal places.
  • Create custom aliases via sed (e.g. I prefer typing "mean" instead of "avg")
  • Bind this command to a shortcut.

Implementing these features could look something like this in your .vimrc:

:noremap <leader>mm
    \ :s/ = .*//e<cr>
    \0y$
    \!!sed 's/mean/avg/;s/^/round(/;s/$/,3)/'<cr>
    \!!calc -p<cr>
    \Pa = <esc>hh

Unfortunately inline comments are not supported natively in vim, but those lines do the the following:

" Remove existing equal signs and results
" Copy the expression
" Create aliases and round the result
" Calculate the expression and remove leading tab
" Insert the original expression plus an equal sign and move the cursor to just before it

Now you can press you mapped shortcut when the cursor is on a line and get the results at the end!

enter image description here

(my leader key is space in the animation)

3
  • 1
    !!calc -p doesn't give you any warning and "just works". :-) Jan 25, 2023 at 2:12
  • @JeromeDalbert great, thank you! I simplified my answer to use your tip Jan 26, 2023 at 15:18
  • I do the same, but use dc instead of calc (I'm used to RPN and that command)
    – gildux
    Jan 26, 2023 at 18:53
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 and acknowledge you have read our privacy policy.

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