Tags
arithmetic, builtin, calculator, eval, input, linux, vim
Need a simple calculator during editing a file using vim and don’t want to click away from it? Then first add the following to your ~/.vimrc:
imap <silent> <C-C> <C-R>=string(eval(input("Calculate: ")))<CR>In order to use the calculatorĀ while you are editing some file using vim, hit CTRL-C in the insert mode and then enter what you want to calculate (e.g., 2+3 or 4.5*7.9).
(It) first calls the built-in
input()function to request the user to type in their calculation, whichinput()then returns as a string. That input string is then passed to the built-ineval(), which evaluates it as a Vimscript expression and returns the result. Next, the built-instring()function converts the numeric result back to a string, which the key-mapping’s<C-R>=sequence is then able to insert.
Reference: here.