TextMate Math Bundle Basics
Since Allan is in New Zealand until February (say hi to my sister), I guess it's up to TextMate users to do some blogging on his behalf. The Math bundle is easily overlooked but it's actually incredibly handy.For example, I have this directory with some log files in it:
First I copy and paste the entire contents into a blank TextMate window:
Then I hold down Option to activate column selection and remove the bits I don't want:
I press Control-Option-Q to remove the line breaks:
Now I use the Math bundle items which are activated with Control-Shift-C. I just choose Add Numbers in Line:
Which gives me this:
Now that's pretty good, but it would be great if it would add the thousands separators for me. Let's open the Bundle editor and view the source for this command:
I did a web search and found a Ruby one-liner that can do the proper formatting for me:
def ts(st)
st.to_s.gsub(/(\d)(?=(\d\d\d)+(?!\d))/, "\\1,")
end
So I'll just add that on to the end of the command source:
And re-run the command:
Perfecto.
Extra Notes
I also could have extracted just file size column programmatically instead of using option-select. That's done with "Filter Through Command..." activated with Option-Command-R:
I just use a short awk command to do the work:
awk '{print $5}'
Also, I should mention that Monokai is now my favorite TextMate theme:
The good stuff.

TextMate Math Bundle Basics
Posted Dec 5, 2006 — 9 comments below
Posted Dec 5, 2006 — 9 comments below
Sam Penrose — Dec 05, 06 2578
Scott Stevenson — Dec 05, 06 2583
The goal was just to demonstrate how to use the Math bundle in general.
Aitor Garca Rey — Dec 12, 06 2647
ummm....
ls -lia ^R ?
:-)
Scott Stevenson — Dec 12, 06 2648
That only works if TextMate is running on the machine with the log files. :)
Jacob Rus — Dec 14, 06 2696
Scott — Dec 20, 06 2775
I can think of a lot of apps better suited for doing simple math than a text editor. Why not save the output as text and import it into Excel? It'll give you the commas for free and can even graph it.
Plus as others have already pointed out, there are plenty of shell commands to do this. In fact, if you're going to use that much Ruby code to format it, why not just do the whole thing in a one liner? (I'm faster at perl than ruby)
perl -e 'map $t += -s, @ARGV; $t =~ s/(\d)(?=(\d\d\d)+(?!\d))/$1,/; print"$t\n"' *
However, since I'm being a bit of a damp rag, let me add that I defintitely do appreciate your posts your posts here and on CDC. Thanks!
Scott Stevenson — Dec 20, 06 2780
Possibly, but I'm not suggesting you fire up TextMate just to do a calculation.
Why not save the output as text and import it into Excel?
Hmmm, slick native editor I spend all day in or a big hulking spreadsheet app that eats up a ton of memory and has little value to me. Is this a trick question? :)
Plus as others have already pointed out, there are plenty of shell commands to do this
Calculating log files was just a contrived example. A convenient set of numbers.
In fact, if you're going to use that much Ruby code to format it, why not just do the whole thing in a one liner?
I don't quite follow. The Ruby code is shorter from what I see. The function definition bit isn't needed:
st.to_s.gsub(/(\d)(?=(\d\d\d)+(?!\d))/, "\\1,")
Maybe I missed your point?
Scott — Dec 22, 06 2820
Just how old is your system? Excel launches a lot faster on my MacBook than it would take me to hunt through the Math Bundle, Google for a Ruby one-liner, option-click click-n-drag across unwanted text columns, delete unneeded line endings... etc. In fact, one of the issues I've always had with text editors is that they quickly blimp up to include features that have nothing to do with text editing (c.f. emacs and vim). To me the Mac approach has always been lots of different apps working in concert rather than the monolithic you-should-never-need-to-leave-this-app approach that so many Windows apps take.
I don't quite follow. The Ruby code is shorter from what I see... Maybe I missed your point?
I was referring to the full Ruby code that made up the command, which also includes this:
print " = " + $stdin.read.scan(/\b([0-9]+(?:\. [0-9]+)?)\b/).inject(0.0) { |t,s| t + s[0].to_f() }.to_s.sub(/.0+\z/, '')
My one liner replaced everything right down to the "ls -lF". Also fyi, I forgot a trailing "g" in my substitution (right before the "; print") which is necessary if the total is >= 1,000,000.
Scott Stevenson — Dec 22, 06 2822
Good thing you don't have to do that each time. :) I guess I'm just not Excel person.
lots of different apps working in concert rather than the monolithic you-should-never-need-to-leave-this-app approach
Assuming you're suggesting TextMate is one of these apps, I think you've mischaracterized it. If you haven't already, give TextMAte a try and see what you think. I think Allan has done a fantastic job of creating a very shallow hierachy in which the complexity scales linearly.
Really, it was just a fun post to show what's possible and demonstrate modifying a bundle item. I never meant it to actually be a way to add up log file sizes.