26

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

26

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.

2
  • What is <CR>? May 24, 2017 at 16:13
  • 2
    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
6

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

CRTL+r=7*7
ENTER
1
  • Ok, it's fine. You wer usint the expression register :)
    – rbernabe
    May 24, 2017 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.

0

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 and hit Enter. You will see that it inserts the correct answer together with a warning:

    7
Unable to associate stdin with /dev/tty

To improve this solution we can

  • Filter out the warning using sed (or vim commands)
  • 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 \| sed 's/  //'<cr>
    \Pld/tty/e<cr>
    \i = <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
" Remove the warning message from calc
" Insert 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)

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.