Wednesday, August 25, 2010

Emacs Quickie: Adding up a column of numbers

The other day I wanted a quick way to sum up an emacs buffer full of numbers. Surely, there must be a function that does such a thing out there? Of course there is. And it's already installed. The answer is calc - one of the coolest parts of emacs.

Calc, as the name suggests is a calculator and one of gems of emacs. I like using it because it uses RPN notation, which makes it unambiguous to use. And the trail window makes it easy to see what numbers added up to what. And of course, not having to switch to another application, means I can do my math and get back to programming sooner (and without touching The Rodent)

But the way I use calc only lightly scratches the surface of its functionality. What I needed for solving the above conundrum was in the manual under grabbing from other buffers. Specifically:

  • calc-grab-region: Slurp in a set of numbers into a Vector structure in calc. And once you've got your vector of numbers you can do interesting things with it.
  • calc-grab-rectangle: Same as calc-grab-region but with rectangles. (You do use rectangles -- right?)
  • calc-grab-sum-down: Does just what the name suggests and just what I needed.

In classic emacs fashion, you can accomplish the same thing as calc-grab-sum-down by typing:

 M-# r V R : + 

Obvious, right?

Seriously, every once in a while I look at the calc manual and think to myself, I really need to do more math so I can play with more of these features.

No comments:

Post a Comment

LinkWithin

Related Posts with Thumbnails