SumOfSelection : Calculate sum of visual (block) selected numbers

 created by Ivo van Kamp script type utility description 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 with the desired key sequence:     vmap SumOfSelectionLastSelection     imap 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 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 Shorthand:            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). Links: 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

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
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