sponsor Vim development Vim logo Vim Book Ad

SumOfSelection : Calculate sum of visual (block) selected numbers

 script karma  Rating 101/392, Downloaded by 4740  Comments, bugs, improvements  Vim wiki

created by
Ivo van Kamp
script type
This plugin sums up a visual (block) selection of numbers and echoes the result.

Arithmetic operators: ^ ** * % / - + (). Math functions: sqrt, log, log2, log10,
exp, abs, sin, cos, tan, base. Hex numbers start with 0x, and binary numbers
with 0b.

Math functions can be abbreviated to the first letter of the function name,
except r=sqrt, l2=log2, l10=log10.

Without an operator the addition sign is added between numbers. Characters not a
number, operator or math function are ignored, as well as any character between
brackets to support commenting.

After visual (block) selection press 't' to echo the sum on the command line. In
insert mode press Alt-= to insert the value of the last expression found on the
current line.

If you want to use other keys to calculate the sum, please add the following to
your .vimrc, and replace <A-a> with the desired key sequence:

    vmap <A-a> <PLUG>SumOfSelectionLastSelection
    imap <A-a> <PLUG>SumOfSelectionLastExpression

The number of decimals can be configured by adding:

    let g:sumofselection_nrofdecimals = [Nr of decimals]

A negative number means full precision. The default value is 2 decimal places.
The rounding method is set to 'common' (i.e. half round up).

The result is stored in the unnamed register "" and register "0.  You can paste
the result in normal mode with p or "0p. And in insert mode with <C-r>0. The sum
is stored in register "s.

Example usage in insert mode:

              2^10 = [ Press Alt-= ]
Result:  2^10 = 1024
              2^10 = 1024 / 8 = [ Press Alt-= ]
Result:  2^10 = 1024 / 8 = 128

Example of possible sums for visual block selection:

           (10          12 * 5.5     12.52
            20          25 * 2.5     17.59
            30)*2     32 * 4.5     15.45


           r144 / r16  =  sqrt(144) / sqrt(16)
           l2               =  log(2)
           l2 0x400   =  log2(0x400)
           b1966,62  = Vi
           b2^10,16  = 0x400
           b0xf,2       = 0b1111

Please note that due to regex restrictions brackets can't be used inside the
argument to log2 and log10 e.g. l2(r(64)) != l2(r64).


Using vim as calculator http://vim.wikia.com/wiki/VimTip73
VimCalc interactive calculator vimscript#3329
HowMuch : A Vim calculator for visual selections vimscript#4761
Scientific Calculator http://vim.wikia.com/wiki/VimTip1235
Crunch : An easier way to perform calculations inside Vim vimscript#4686
install details
Copy script file into Vim's plugin directory.
Plugin requires Vim to be compiled with the +perl feature.

rate this script Life Changing Helpful Unfulfilling 
script versions (upload new version)

Click on the package to download.

package script version date Vim version user release notes
SumOfSelection-1.16.vim 1.16 2017-06-26 7.0 Ivo van Kamp - Added base[decimal/hex/bin, base n] function
- Refactoring
SumOfSelection-1.15.vim 1.15 2017-06-10 7.0 Ivo van Kamp Small regex bugfix
SumOfSelection-1.14.vim 1.14 2017-05-26 7.0 Ivo van Kamp - Added support for basic math functions: sqrt, log, log2, log10, exp, abs, sin, cos, tan
- Added support for hex strings with 0x, and binary strings with 0b
SumOfSelection-1.13.vim 1.13 2017-05-18 7.0 Ivo van Kamp Small regex bugfixes + refactoring
SumOfSelection-1.12.vim 1.12 2017-05-11 7.0 Ivo van Kamp - Regex refactoring
- Improved error handling
SumOfSelection-1.11.vim 1.11 2017-05-09 7.0 Ivo van Kamp - Refactoring
- Added support for inline comments
SumOfSelection-1.10.vim 1.10 2017-05-07 7.0 Ivo van Kamp - Improved code efficiency
- Added configuration option to set nr of decimal places
SumOfSelection-1.9.vim 1.9 2017-05-04 7.0 Ivo van Kamp - Added function to find the first equation in front of the cursor after an equality sign
- Added plugin support
- Refactoring, small bugfixes and improvements
SumOfSelection-1.8.vim 1.8 2017-05-01 7.0 Ivo van Kamp Refactoring + small bugfixes
SumOfSelection-1.7.vim 1.7 2017-04-30 7.0 Ivo van Kamp - Added support for parenthesis
- After calculation the result can now be pasted
SumOfSelection-1.6.vim 1.6 2017-04-29 7.0 Ivo van Kamp Added support for arithmetic operators
SumOfSelection-1.5.vim 1.5 2017-04-29 7.0 Ivo van Kamp Added constant to configure nr of decimals
SumOfSelection-1.4.vim 1.4 2017-04-28 7.0 Ivo van Kamp Initial upload
ip used for rating:

If you have questions or remarks about this site, visit the vimonline development pages. Please use this site responsibly.
Questions about Vim should go to the maillist. Help Bram help Uganda.
Vim at Github