This is my list of TextMate‘s keyboard shortcuts that are very useful for Ruby and Rails developers, but are not used as widely as they should (according to my very scientific observation on a very representative sample, i.e. my colleagues). I skipped all the obvious ones (like “open file”, “save file”, “go to next window”, “close window” etc.) that most people use anyway. My list includes shortcuts that are very useful but sometimes might be hard to grasp at first or might require a little explanation.



Built-in

The following shortcuts are either built-in TextMate function or belong to built-in bundles. You don’t have to install anything for them to work.

alt-cmd-down — Go To Alternate File

This is one of the most valuable commands and it makes TextMate almost as smart as an IDE (which it isn’t (which is good)) providing quick navigation between related files. This command behaves differently depending on where you are. If you use it in a model class, it opens the unit test (or propose to create one if there isn’t). From unit test it will jump back to the model. If you use it in some controller’s action, it will jump to corresponding view file (and back). Using it on controller’s name takes you to the functional test (and vice versa). And so on. There is also a not-so-smart counterpart, alt-shift-cmd-down , which lets you select one of the related files to open.

ctrl-shift-D — Duplicate Line/Selection

This is faster than cmd-C / cmd-V , provided that you want the copy to be inserted just after the original. It’s nice that this macro is smart: normally it will duplicate line but when you select something, only the selected text is duplicated.

alt-cmd-] — Align Assignments

This is great for prettifying code. Whenever you have some consecutive assignments, for example:

first = 3 second = "some string" third = :lorem_ipsum last = "but not least"

…just place cursor on one of the lines and press alt-cmd-] to have those assignments nicely aligned:

first = 3 second = "some string" third = :lorem_ipsum last = "but not least"

By a coincidence (or by design, I don’t know), this also works with multi-line hashes, for example:

link_to "Click me", some_path, :class => "some classes", :rel => "iframe", :title => "Some title"

The “rockets” => were also auto-aligned with alt-cmd-] .

The command is not very smart and aligns all the consecutive lines containing equality sign which sometimes leads to undesirable effects (but nothing a cmd-Z couldn’t fix). To prevent this select only those lines you want aligned.

ctrl-cmd-T — Select bundle item

If you know that some command, snippet or macro exists, this command helps you find the shortcut for it. This is also great for finding snippets of code that you type again and again. Tired of typing validates_presence_of time and time again? Fret no more. Just fire “Select bundle item” dialog, type first letters of it (like vpo ) and discover that (thanks to Rails bundle) it’s just vp<Tab> .

Vertical blocks

Most of the “serious” editors for developers support them but in TextMate they are as good as in Vim. This means that not only you can select vertical blocks (with alt-dragging), cut, copy and paste them, but you can also edit them! So if you have to correct something over several aligned lines, you can do it at once. For example, let’s say that we have some code like this:

foo.first = ... foo.second = ... foo.third = ...

and you want to change all the foo ‘s into bar ‘s. Of course, you could use Search and Replace, but vertical blocks are much faster and easier in this case. Just select the block of foo ‘s (with alt-dragging) and start typing bar . You’ll see that foo ‘s disappeared and bar ‘s are typed into all the selected lines at once. Aren’t vertical blocks wonderful?

Search and Replace

When I first started using TextMate, I thought that the Search and Replace dialog is not very developer friendly. But then I learned the shortcuts to various S/R subfunctions that allow you to perform most of simple S/R operations without even opening the dialog. For example, if you want to replace foo ‘s with bar ‘s (and have both words somewhere in the editor), the procedure is:

select foo (with double-click or ctrl-W ) press cmd-E (that’s “Use Selection for Find”) select bar press shift-cmd-E (that’s “Use Selection for Replace”) press cmd-G to Find Next occurrence if you want to replace it, press alt-cmd-F which is the Replace & Find command and will, quite appropriately, replace the occurrence and find next one if you don’t want to replace it, press cmd-G again.

This looks complicated, but once you master the shortcuts, it will be much faster for you than using the S/R dialog. There are more of them, for a full list refer to Edit→Find menu in TextMate.

Regular expressions

It’s not that you shouldn’t ever use the S/R dialog. If you did you couldn’t use another powerful feature — regular expression support for search and replace. If you grok regexps (and you should) you can quickly perform some tedious editing tasks that otherwise could take you many minutes (not to mention the bugs you would introduce). I won’t go into details here but it’s worth noting that in the Replace edit box you can use $1 , $2 etc. to refer to groups captured in parentheses in the Find box.

ctrl-S — Incremental search

Another searching function, this time it’s for Emacs lovers. This one is a little bit confusing at first (especially if you come from Windows and want to save a file, heh), because the text you type after hitting ctrl-S appears in the status bar at the bottom of TextMate window. No wonder it’s hard to spot when you hit it by accident. But once you overcome this difficulties, this is one of the fastest ways to locate a method or variable in a file.

ctrl-{ — Toggle ‘do … end’ / ‘{ … }’

In Ruby, the difference between do end and { } blocks is very small — they differ only in the precedence, which for most practical purposes means that any additional function arguments must be in parentheses for { } while do end does not require them.

There is however quite a strong convention that { } should not be multiline (i.e. contain more than one statement/expression) while do end should rather be longer than one line. There is also another convention that if you call some methods on the block result, you should use { } because it looks better (compare: arr.map { ... }.flatten versus arr.map do ... end.flatten ).

The interchangeability and all of these conventions mean that you’ll probably need to convert { } blocks to do end blocks (and vice versa) quite a lot. And that’s why this shortcut is very helpful. Be warned however that it’s only a (not so) simple search/replace macro so it works for about 80% of cases and the more complicated ones will confuse it. BTW the shortcut for it is ctrl-{ which means that you’ll actually have to press three keys: ctrl-shift-[ .

RubyAMP

And now for a couple of shortcuts from the brilliant RubyAMP package. If you don’t use it, you better start — it’s loaded with useful shortcuts and macros. The ones I use all the time are:

cmd-; — Complete word from all open documents

This is word completion on steroids. It works like TextMate’s esc key but searches all open documents. Extremely useful.

alt-shift-m/c/f — Grep for method/class/file

Another set of IDE-like shortcuts. alt-shift-m locates the definition of the method under the cursor (also works with relations like has_many and model attributes if you use annotate_models plugin). Use alt-shift-c to jump to class or module under the cursor (also works with constants). And finally alt-shift-f opens a file with a name that matches what’s under the cursor.

ctrl-alt-L — Pretty align

This is a more sophisticated version of TextMate’s built-in Align Assignments. It requires you to select the lines you want aligned (no auto-detection) and then enter what should be aligned (e.g. = , => or : ). I generally use it when Align Assignments fails.

alt-e — Show Open Windows

Great when you open so many files simultaneously that their tabs don’t fit in TextMate window.