Noel Rappin Writes Here

Text And Mate

Posted on February 10, 2011


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.



Comments

comments powered by Disqus



Copyright 2024 Noel Rappin

All opinions and thoughts expressed or shared in this article or post are my own and are independent of and should not be attributed to my current employer, Chime Financial, Inc., or its subsidiaries.