Vim as a calculator

Simple config that makes vim a (usable) calculator

A calculator is one of those little tools that are both essential for some tasks and a nuisance to bring up. If you use vim this problem should be addressed forever with a single line of configuration:

nnoremap <silent> = :call execute("put =" . getline("."))<CR>

With this in your .vimrc, you will be able to write a line in any buffer containing mathematical expressions, press equal sign while on that line in normal mode, et voilĂ , a new line will appear below with a result of the previous mode. If you want you can continue calculations by adding more stuff to the newly created line and pressing the equal sign again. Lines before the current one are basically a history of calculations.

Of course this is much more than a calculator, this is basically a vimscript expression result printer, letting you run functions inside, like log, hostname or rand. Type :help functions in vim to get the full list. Just be careful not to run this on something that can act maliciously, remember that vim can for example delete files, so you might want to put it under a harder to press combination.

Comments? Suggestions? Send to
Published on 2019-12-21