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 orctrl-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.
February 15th, 2010 at 14:09:27
I can’t believe I didn’t know TextMate could do incremental search. Thanks for the list!
February 15th, 2010 at 17:42:38
Awesome. Didn’t know TextMate could do vertical blocks. This is one of my favorites feature in Vim. Another one of my favorite TextMate commands is using CMD-[ or CMD-] to increment or decrement the tab alignment on a block of code.
February 15th, 2010 at 18:06:14
Good article. I didn’t know about a couple of these. It’s amazing how much of a timesaver these well-thought-out commands are.
BTW, where’s the alt key? I only see it in small print above the option key to placate Windows switchers :-)
February 15th, 2010 at 18:27:35
Heh, you got me here. Yes, it’s Option key in Apple’s lingo :) Although somehow they label it “alt” on all the mac keyboards I’ve been using :)
February 15th, 2010 at 18:30:03
[…] TextMate shortcuts you should be using « require ‘brain’ – Awesome list of TextMate timesavers […]
February 15th, 2010 at 21:34:25
Technically, I believe that the vertical blocs is a property of OSX. You can do the same thing in Text Edit (though not as nicely). Its not universally available in all apps but try it in your favorite app (probably only those written for 10.5+… I think its a newer addition)
February 16th, 2010 at 02:39:44
[…] TextMate shortcuts you should be using This is my list of TextMate’s keyboard shortcuts that are very useful for Ruby and Rails developers, but are not […] […]
February 16th, 2010 at 05:02:15
I love TextMate’s vertical blocks, and for a couple of years now have been dying to figure out a way to do it with just the keyboard. Three or four times I’ve done it by accident, so I knew it was possible — and a month ago I cracked the code:
Select the range you want to do block edit with normally, and then just press option once. Once you do that, magic will happen.
February 16th, 2010 at 06:24:27
“alt-cmd-] ” is awesome, I had no clue. Another cool tool (as a bundle) is the Ack tmbundle. It does a project search using Ack and is significantly faster than the textmate ‘Find in Project’ and is the replacement for Ruby AMP’s ‘grep in project’: http://github.com/protocool/ack-tmbundle
February 16th, 2010 at 09:05:34
I just made it to Top 100 Posts on WordPress for February, 16th on… 100th place :)
Thanks to all that visited and commented!
February 16th, 2010 at 12:40:54
Thanks for this – several of these have now been scribbled onto a post-it note and stuck to my monitor :)
February 16th, 2010 at 17:24:05
Awesome stuff here. “Show open windows” never knew that one existed
February 17th, 2010 at 02:09:58
[…] TextMate shortcuts you should be using « require ‘brain’ (tags: textmate shortcuts) […]
February 27th, 2010 at 00:06:20
[…] Some really great tips for Textmate users – Link […]
March 4th, 2010 at 20:46:24
Thanks.. these are great time savers
March 8th, 2010 at 00:44:09
thanks.. nice info
March 13th, 2010 at 10:47:32
[…] Handy article on Ruby on Rails related TextMate shortcuts: https://szeryf.wordpress.com/2010/02/15/textmate-shortcuts-you-should-be-using/ […]
March 16th, 2010 at 17:45:33
Alt-E is exactly what I’ve been looking for, but it types the standard accented e on my keyboard (é). Is there a specific way it should be typed to show open windows?
March 16th, 2010 at 17:56:29
Argh…it’s in the separate RubyAMP package. Didn’t catch that subheading the first time through.
March 16th, 2010 at 18:01:24
I don’t know any other way to type it :) But you can remap the shortcut to some other key-combo. I did it for myself, too, because normally Alt-E is mapped to letter ę in Polish keyboard layout. To remap it, fire Bundle Editor (Ctrl-Alt-Cmd-B), find RubyAMP bundle, click the triangle on the left to unfold it, find Show Open Windows command, click it and in the lower-right corner there’s an editbox for shortcut. I mapped it to Alt-Cmd-E as this combination seemed free.
April 4th, 2010 at 03:17:10
If you’re documenting Mac software, it’s “option,” not “alt.”
April 12th, 2010 at 03:29:32
[…] Text editing TextMate tips. […]
June 24th, 2010 at 11:59:27
Thanks for the post: it’s what I was looking for! I got one extra question: I normally use notepad++, which has a great shortcut for code comment (ctrl + shift + q), and I can’t find anything similar in TextMate. Any idea? Thanks again!
June 24th, 2010 at 16:52:27
It’s Cmd-/ (Comment Line) and Opt-Cmd-/ (Insert Code Block). If you want to find the key combination, open Select Bundle Item (Ctrl-Cmd-T) and just start typing whatever you think it may be named :)
June 28th, 2010 at 23:14:41
Thanks man! Very useful: I’ve just started using it, because I don’t have the heart to kill a line of code, so I comment them…
July 22nd, 2010 at 14:17:33
Vertical blocks are quite cool as long as your code all lines up. I’ve never found a program that does blocks as well as E though. You can set multiple caret locations and edit at multiple points at the same time. A vertical block is really just a subset of multiple carets. Unfortunately, E is windows only so no good for me anymore.
December 4th, 2010 at 21:47:30
Was looking for other keyboard shortcuts not shown here (navigating between project windows), but found some nice shortcuts here anyway, like align assignment. Thanks!
December 4th, 2010 at 21:58:51
@Dan, do you mean cmd-` ?
December 9th, 2010 at 19:21:48
Useful list, thanks!
Another favorite of mine is Cmd+Opt+A (invoked after selecting the lines you want it to affect). This is, essentially, column edit mode, but offset from the *end* of the line, instead of from the beginning. Useful when you want to add another argument in a line of function calls right before the ) character, or append some suffix to several lines at once.
It’s also worth pointing out what makes Cmd+Option+] work: it takes a selection (or the entire document), pipes it into a script (Ruby in this case, but you can use anything you’re comfortable with), and replaces the selection with the output of that script. This is an extremely powerful concept once you get used to it; for example, I used it to write a shortcut that aligns all the ‘|’ characters in Cucumber tables. (That script then got modified and included in the Cucumber bundle.) I have another one that inserts sequential numbers on subsequent lines (which I sometimes use in conjunction with column edit mode), one that strips trailing whitespace from the selection, one that joins lines together and adds commas between them…. and the list goes on. See: https://github.com/geeksam/slg-textmate-bundle for more.
December 10th, 2010 at 01:41:00
[…] via TextMate shortcuts you should be using « require ‘brain’. […]
March 9th, 2011 at 21:21:47
What is the key shortcut to select vertical block of text ?
thanks,
-sunny
March 9th, 2011 at 21:41:25
Sunny, you can’t select a vertical block with the keys only, you need to drag while pressing the alt/opt key.
Alternatively, you can select a “normal” block with e.g. shift and cursor keys, then tap alt/opt key once to switch it to vertical mode.
September 2nd, 2011 at 23:34:47
Please don’t try and tell me that vertical blocks in textmate are as good as in vim.
September 3rd, 2011 at 10:23:50
NickNick: I used both Vim’s and TextMate’s vertical blocks and in my opinion their functionality is similar. If you see some differences, please share…
September 8th, 2011 at 10:02:00
We did a short little video on some awesome shortcuts: http://bit.ly/ppD0ij
-enjoy :)
December 19th, 2011 at 23:41:42
[…] TextMate shortcuts you should be using Share this:EmailLike this:LikeBe the first to like this post. This entry was posted in Craftsmanship, Mac, Software and tagged Craftsmanship, shortcuts, textmate by Lance Willett. Bookmark the permalink. […]
September 9th, 2012 at 07:26:50
[…] TextMate Shortcuts szeryf’s list of very useful keyboard shortcuts This entry was posted in Uncategorized and tagged links, TextMate. Bookmark the permalink. ← TextMate 2 […]
July 7th, 2014 at 10:07:04
[…] found a number of great posts and resources :Essential TextMate Shortcuts, Tips and Techniques and TextMate shortcuts you should be using, also the PDF version of the Textmate cheat […]