SumOfSelection : Calculate sum of visual (block) selected numbers
script karma 
Rating 10/80,
Downloaded by 146

Comments, bugs, improvements

Vim wiki

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. 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 <Aa> with the desired key sequence:
vmap <Aa> <PLUG>SumOfSelectionLastSelection imap <Aa>
<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 <Cr>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)


install details 
Copy script file into Vim's plugin directory.
Plugin requires Vim to be compiled with the +Perl feature. 

script versions (upload new version)
Click on the package to download.
SumOfSelection1.14.vim 
1.14 
20170525 
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 
SumOfSelection1.13.vim 
1.13 
20170518 
7.0 
Ivo van Kamp 
Small regex bugfixes + refactoring 
SumOfSelection1.12.vim 
1.12 
20170511 
7.0 
Ivo van Kamp 
 Regex refactoring
 Improved error handling 
SumOfSelection1.11.vim 
1.11 
20170509 
7.0 
Ivo van Kamp 
 Refactoring
 Added support for inline comments 
SumOfSelection1.10.vim 
1.10 
20170507 
7.0 
Ivo van Kamp 
 Improved code efficiency
 Added configuration option to set nr of decimal places 
SumOfSelection1.9.vim 
1.9 
20170504 
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 
SumOfSelection1.8.vim 
1.8 
20170501 
7.0 
Ivo van Kamp 
Refactoring + small bugfixes 
SumOfSelection1.7.vim 
1.7 
20170430 
7.0 
Ivo van Kamp 
 Added support for parenthesis
 After calculation the result can now be pasted 
SumOfSelection1.6.vim 
1.6 
20170429 
7.0 
Ivo van Kamp 
Added support for arithmetic operators 
SumOfSelection1.5.vim 
1.5 
20170429 
7.0 
Ivo van Kamp 
Added constant to configure nr of decimals 
SumOfSelection1.4.vim 
1.4 
20170428 
7.0 
Ivo van Kamp 
Initial upload 
ip used for rating: 54.196.107.247
