sponsor Vim development Vim logo Vim Book Ad

visSum.vim : Computes sum of selected numbers

 script karma  Rating 79/33, Downloaded by 2447  Comments, bugs, improvements  Vim wiki

created by
erik falor
script type
It is something so trivial, I can't believe it hasn't been done before.
After looking for an hour I couldn't find a plugin to do this without relying on external programs, so here is my offering, 100% pure vimscript.

Highlight some lines containing numbers, and use <Leader>su (usually that's \su) and the sum appears in the message line.  

It works by grepping each line of your selection for the first number it sees, and adds that to the running total.

Visual Block support added in version 0.2.

Feedback to g?? ArjBsnyFbCe@tzNnvZy.pbz - NOSPAM
install details
Drop visSum.vim into your ~/.vim/plugin directory.

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
visSum.vim 1.2 2015-08-19 7.0 erik falor This version (1.2) contains a fix for a bug reported by Fabio Inguaggiato.
Thanks, Fabio!
visSum.vim 1.1 2015-06-17 7.0 erik falor Fixes a bug discovered by Markus Weimar.
visSum.vim 1.0 2012-07-18 7.0 erik falor After nearly five years, I've finally updated this plugin to support floating-point numbers.
I couldn't have done it without the patch submitted by Christian Mauderer.  Thanks, Christian!

The plugin will work equally well whether or not your Vim supports the +float feature; if you don't have +float the old behavior is retained.  If you sum a column of numbers and the fractional part is insignificant, an integer is returned.  I went out of may way to make that happen.  If you don't like it, please let me know and I'll change it.  For what I perceive to be the most common use-case, this shouldn't be problematic.

Vim can also represent floats in scientific notation: presently this is unsupported.  If you'd like this feature, I'd recommend following Christian's lead and submitting a patch.
visSum.vim 0.3 2007-11-01 7.0 erik falor Stores sum in buffer variable b:sum.
:VisSum command can take a register name as an argument to store the sum.
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.