Summing a column of numbers in Emacs

Emacs continues to amaze me every single day. It's getting near tax time and today I was summarizing various expenses in an ascii file. Why ascii and not Excel or some other spreadsheet? Well, for one they are overkill when all you want is a simple column of numbers and a sum. Secondly, my summary as an ascii file will be viewable ten years from now without me having to get the latest version of MS Office or whatever and worrying about data migration. Text files work today and will work fine into the future.

So I'm breaking things up into categories and looking at the columns of numbers and thinking to myself, text files are cool and all, but what am I going to use to add them up? And a thought occurs to me, hey I'm in Emacs. The darn tool has everything else, I bet it has a way to calculate something like this. And lo and behold, it does. There's a built in calculator in Emacs that lets you do all kinds of calculations.

In my case, I could select a rectangular column of numbers. Do C-u C-x * r and calculator would grab the rectangular column and create a vector on the stack. Trouble is, you want the numbers as individual elements so you can sum them. No problem, in the Calc buffer type V u to invoke the calc-unpack function and it splits the numbers in frames on the stack. Now just iterate through by typing the + key and it will add them all up.

I'm sure there are even shorter ways to do this, but for now this simple set of commands saved me from having to fire up Office. In fact, Emacs is what I'm using to write and publish this little blog entry. Mmmm I wonder if it might also be good for writing code ...

tags: emacs


comments powered by Disqus