Wednesday, May 16, 2007

The TextMate Files: Cool Snippet/bash Interaction

As you may have noticed, TextMate snippets are really just a wrapper around a bunch of serious bash-fu: most of the really cool stuff you can do in snippets is actually bash code (with some helpers). I found a great web link that offers a really good tutorial about some of the fu here, along with some of the helpers.

Here's an example of one of the helpers. The problem: I need to annotate a bunch of Ruby code in RDoc, and I want to add a link to the bottom of the RDoc in every file that points to the source directory of the file in question. But, it needs to be a relative path. There is probably some really much cooler way to solve this, but here is the snippet I created:

---
source: link:../${TM_FILEPATH/\/Users\/jNf\/Documents\/dev\/ruby\/meta-programming\///}

This says to add a horizontal rule (the "---" in RDoc), then do a substitution for the built-in TM_FILEPATH variable, substituting the full path stuff (up to the file name) with nothing. The general syntax for the substitution is ${VARIABLE/pattern/substituion}. The resulting link looks like this:

---
source: link:../16.delegation/forwarding.rb
Which is exactly what I want.

No comments: