Rails Test Prescriptions Blog

Keeping Your Application Healthy Since 2008

Text And Mate

After a long time bouncing back and forth, I’ve come back to TextMate as my main editor. I realize that’s starting to sound almost old-school these days, but it still works the best for me.

What I’ve come to realize about TextMate versus, say, Vim, or RubyMine is that a) this is a genuinely personal decision and different people are just more comfortable with some tools than other and b) it comes down to what each tool makes easy and how useful that is.

For instance, RubyMine. RubyMine makes navigating a project super easy, which is great, since I do that all the time. It also makes refactoring easy, which is less useful because in practice I use the automated refactoring less. Vim makes manipulating text, if not easy, at least powerful, but again, I find myself doing that less. And the thing that Vim makes hard, having to keep track of modes, absolutely drives me crazy.

Anyway, TextMate. TextMate makes creating new snippets and triggers very easy, and doesn’t make anything particularly hard. That said, I have seen in some of my pairing around that a lot of Ruby developers don’t know about all the tools that give TextMate some of the features of RubyMine and Vim. So here are a dozen or so things that you can to to make TextMate work for you.

AckMate

Install the AckMate plugin. AckMate is a very fast project-wide text search with a nice interface. It’s about a gazillion times nicer than TextMate’s internal project search.

Ruby Amp

Install the ruby-amp bundle. Ruby-amp gives you a number of features. The one I use most often is a command-; shortcut for auto-completion against symbols in all open files. Very useful. You also get navigation to method, module, or class definitions with a keyboard shortcut.

Search Bundle Commands

Which reminds me, control-command-t is maybe the most important keyboard shortcut to learn, it gives you a pulldown menu of all bundle commands that you can fuzzy-text search. Changed how I work, and opened up a lot more of the power of the various bundles.

Zen Coding

Install the Zen Coding bundle. Zen Coding has a lot of features, but at core, it’s sort-of a meta-shortcut for outputting HTML or XML. So you type in something like div#highlight>ul>(li*3>p), press command-e and out comes fully formed HTML:

  <div id="highlight">
    <ul>
      <li>
        <p></p>
      </li>
      <li>
        <p></p>
      </li>
      <li>
        <p></p>
      </li>
    </ul>
  </div>
	

It’s wildly awesome anytime you have to write highly structured text, not just HTML, but also XML or DocBook or anything like it.

Column Selection

Holding down the option key when you select text, allows you to select text in a rectangle, what TextMate calls “column selection”. What can you do with this? Insert a prefix on a bunch of lines. Remove an entire column of a Cucumber table in one action. Remove the colon at the beginning of a list of symbols. And that’s just the stuff I did with it today…

Peep Open

PeepOpen costs a few bucks (technically $12), but it’s a somewhat cleaner version of TextMate’s file dialog, with slightly faster file search. The main downside is that it’s still kind of beta-y in that it occasionally hangs.

Make Snippets

Learn to create your own snippets. For example, I use the RSpec let command all the time. The RSpec bundle doesn’t have a snippet for let. Sob. Go to the bundle editor, select the RSpec bundle, choose create snippet from the bottom left, and put let(:$1) { $0 } in the text box. Set the tab completion to let, optionally set the scope to source.ruby.rspec and boom, good to go. (The dollar signs indicate places where the cursor goes when you press tab, in numerical order, but $0 is last. You can also set defaults if you want.) I’m skimming the surface here, but it’s super easy, and it would have been a real challenge to write RTP without defining custom snippets.

Commands

You can also create commands which run scripts, which can be written in the scripting language of your choice. Like, say, Ruby. You have access to the current file. Also very easy and very helpful. Here’s a dumb one I wrote that takes an HTML link in text and turns it into a Markdown self link. Again, you do this in the bundle editor:

#!/usr/bin/env ruby -w
input = STDIN.read
print "[#{input}](#{input})"

Assign that a key and a scope of text.html.markdown, and set the input to “Selected Text” and the output to “Replace Selected Text”

Here’s a slightly less dumb one that converts soft-wrapped text to hard wrapped text, indented to whatever level the first selected line is. I use this all the time when I write structured text. In

#!/usr/bin/env ruby -wKU

text = STDIN.read
initial_spaces = " " * ((text.size) - (text.lstrip.size))
text = text.strip.gsub(/\s+/, ' ') 

result = "#{initial_spaces}"

count = initial_spaces.size
text.split.each do |word|
  count += (word.size + 1)
  if count > 79
    result += "\n#{initial_spaces}" 
    count = word.size + initial_spaces.size + 1
  else
    result += " " unless result.size == initial_spaces.size
  end
  result += word
end

printf result

Scopes

Learn About Scopes. TextMate is based on the concept of scopes, which are defined by the language files in each bundle and determine what key commands and bundle items are active at any time. This is what lets TextMate syntax color multiple languages in the same file, it knows, for example, that the ERB &lt= marker makes the code enter Ruby scope so that Ruby syntax coloring and commands apply. control-shift-p at any place to show the scopes in play at that point in the text.

I learned about scopes from the TextMate book. It’s a few years old now, and I haven’t read it in a while, but it had a lot of good info on TextMate basics.

Random other Bundles

The default Cucumber bundle has a lot of useful stuff like goto step definition from feature, and so on. The git bundle has a useful visualization of git blame. The subtle gradient bundle has some useful grab bag stuff, including aligning ruby code.

Random other commands

Command-shift-v: paste previous contents of the clipboard

Control-t: transpose the two characters on either side of the cursor.

Command-shift-B: select enclosing brackets

Themes

Here’s a git repo with a jillion TextMate themes. Find one you like. Also, it’s easy to customize themes, especially once you get the hang of scopes and language definitions, since you can define text types in the bundle language definitions. See this article for more.

I hope that helps somebody. If you have a tip I don’t, leave it as a comment.

Advertisements

19 responses to “Text And Mate

  1. Pingback: Tweets that mention Text And Mate « Rails Test Prescriptions Blog -- Topsy.com

  2. Steven February 10, 2011 at 10:31 am

    Thanks for this. I got my first macbook last week and am in the middle of learning what Textmate has to offer so I’ll definitely be using a few of these tips.

  3. Konstantin Haase February 10, 2011 at 12:53 pm

    Also, give Redcar a try.

  4. Chris February 10, 2011 at 1:41 pm

    See this is the conundrum I have right now, I have been using both rubymine and textmate in their trial versions for the past month or two, and I like both. But I don’t like either one enough to purchase one over the other… not sure what to do.

  5. Hussein February 10, 2011 at 1:44 pm

    I am so sad about Allan Odgaard, the developer of Textmate. Textmate is great but there is no progress.

  6. Troy February 10, 2011 at 3:04 pm

    I agree with what you’re saying, but the nth time Textmate crashed and lost work was one time too many.

  7. bryanl February 10, 2011 at 3:17 pm

    I’m not interested in an editor that is no longer improving. Sure textmate 2 is coming one day.

    • noelrap February 10, 2011 at 3:19 pm

      That’s obviously your choice, but since most of the features I use come from bundles, and the bundles are still continuously improving, I don’t see that as a big issue for me.

  8. Caius Durling February 10, 2011 at 3:25 pm

    FYI, you don’t need to do [http://google.com](http://google.com/) in markdown for a href to that uri, you just wrap it in angle brackets: . Works for email addresses too.

  9. yachris February 10, 2011 at 4:22 pm

    For whatever it’s worth, you can do snippets pretty much identically in RubyMine as TextMate. I have them both.

    • noelrap February 10, 2011 at 4:25 pm

      That’s true, although I think it’s a little bit harder to manage them in RubyMine then in TextMate (I remember creating a few for IntelliJ back in the day). RubyMine has a plugin system too, but again, I think it’s a bit more awkward.

      I like RubyMine, the biggest thing keeping me from using it more frequently is a) performance, and b) the fact that TextMate can be used for anything I write, not just code.

  10. Justin Ko February 10, 2011 at 6:15 pm

    So glad to hear that someone else switched back to Textmate. I read the Vim book, and used Vim full-time for two weeks. There are some really cool things about Vim, but in the end it is just way too much key presses for me. I’d much rather lift my hand and move it to the mouse. Also, my fingers started killing me because my hands never left the keyboard! They would just sit there and turn into claws.

  11. Gerard Leijdekkers February 11, 2011 at 2:37 am

    Nice writeup!

    Command-t: transpose the two characters on either side of the cursor.
    should be:
    Ctrl-t: transpose the two characters on either side of the cursor.

  12. Dave Giunta February 11, 2011 at 9:37 am

    Control-Command- allows you to move a selected chunk of text around in the document. I find it perfect for times when I normally would copy/paste. For example, if you have lines 4-6 selected of a document, and you want to move them up to lines 1-3, you just hit control-command-up-arrow 3 times and it moves everything to that point. Same goes if you’ve got characters selected in a line, and you want to move them forward or backward a few characters. I use that a lot when I’m writing javascript functions inside parenthesis where the “fun” snippet puts in a semicolon before the closing parenthesis. I’ll select the semicolon, control-command-right-arrow to move it one character to the right, or outside the parenthesis.

    It actually sounds a lot more verbose when it’s typed out, but I find it to be super handy when editing with TextMate. I missed it so much when I moved to Vim that I had to find a Vim function that did the same thing. The one I found does lines of text no problem, but doesn’t handle characters, which is kind of a shame.

    Which leads me to another note about comparing TextMate to Vim… I really enjoy using Vim since I switched, but I really, really miss the ease with which I could write custom macros, commands and snippets with TextMate. For all the hullabaloo about how customizable Vim is (and it definitely is), TextMate “felt” easier to customize. I’m not sure if that’s just because the Bundle Editor exposed that functionality in a simpler manner, or if I’m just not used to Vim’s customization features yet. But either way, that’s what I miss from TM.

    Nice write-up!

  13. Dan February 11, 2011 at 11:55 am

    No split screen is a deal breaker for me.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: