Friday, 3 August 2012

TextMate productivity tips

If you saw my post about the DOMAssistant bundle for TextMate, you will already know that my favourite coding tool at the moment is TextMate. If you missed that post, well, now you know.
For anyone who is new to TextMate or considering trying it out, I thought I’d share a few tips. These tips are nothing revolutionary for many Textmate users, but nevertheless they make my workdays a little easier. Several of these tips may not be all that obvious, so I hope you will find something new here.

1. Insert close tag

When editing an HTML document, press Opt + Cmd + . to make TextMate automatically insert a closing tag for the element the cursor is inside.

2. Move selection

To move text that you have selected, hold down Ctrl + Cmd and use the arrow keys to move the text up and down (or left and right). If you’re moving text up or down, make sure to select the entire last line or the line break will get left behind.

3. Tab triggered snippets

Type a few characters and hit the tab key. Bam! TextMate inserts anything from a simple tag or method to an entire document. And if you’re not satisfied with the snippets that ship with TextMate you can add your own. Do you find yourself typing a similar pattern several times daily? Make a snippet out of it.

4. Code completion

Instead of typing every letter of every tag, attribute, method or variable, just type the first few letters and hit the Esc key. TextMate will suggest a word for you. Not happy with the suggestion? Press Esc again to get a new suggestion.

5. Go to file

While working on a project I often have many files open in TextMate. Moving between them by choosing a tab can be awkward when there are more open documents than fit in the tab bar. That’s when Cmd + T is very handy. It opens the Navigation - Go To File... dialog box, where you can either choose which file to work on or type a few letters that are part of the name of the file you are looking for.

6. Wrap selection in open/close tag

When you want to wrap some text in an HTML element, select the text and hit Ctrl + Shift + W. TextMate will wrap the selection in a p element. The element name in the opening tag is selected, so if you need a different element, just type it in and TextMate will change the closing tag as well.
Want to wrap multiple lines in separate elements, for instance to create list items? Select the lines and press Ctrl + Shift + Cmd + W and TextMate wraps each line in a list element. Again, if you want a different element just type it and the <li> and </li>tags will be replaced.

7. Move through text efficiently

In TextMate and most other Mac applications, the modifier keys change how the arrow keys work when moving through text. Holding down the Cmd key skips to the beginning or end of a line, and holding down the Opt key will skip to the next “word”.
TextMate extends this by letting you use the Ctrl key to skip to the next word or word separator. That comes in very handy when you want to select part of a variable or method, since TextMate treats dashes (-), underscores (_) and case changes (like in CamelCase) as word separators.

8. Wrap Word/Selection as Link

When you need to link a piece of text, copy the URL, select the text you want to link, and press Ctrl + Shift + L. TextMate will wrap the selection (or the next word if there is no selection) in a link, using whatever is in the clipboard for the value of thehref attribute.

9. Delete whitespace, and only whitespace

When editing files I often find myself deleting whitespace between the opening and closing tags of elements. Holding the option while pressing delete or backspace will remove all whitespace between the cursor and the next “word”. The catch is that TextMate will also remove the end or beginning of HTML tags this way, which is incredibly annoying. But using Ctrl + Opt + Delete will delete all whitespace from the cursor until the next HTML tag without touching the tag.

10. Create a project by dragging files onto TextMate

When you want to work with several files that are related in some way it’s very handy to have them in a TextMate project. The quickest way to create a new project containing the files you need is to select the files in the Finder and drag them onto the TextMate icon in the dock. Remember to save the project file if you want to reopen it later.

11. Indent and outdent text quickly

Select the lines you want to indent or outdent, then press Opt + Tab to indent them orShift + Opt + Tab to outdent them.

12. Macros

TextMate lets you create macros that combine multiple commands, which can be very useful. One macro that I use a lot when writing posts for this blog will turn a selected block of code into an ordered list with code elements and classes that match the indentation level.

Just scratching the surface

Those are just a few of the TextMate tricks I use every day. There are many, many more, and I’m sure most TextMate users would pick different favourites.
Posted on November 25, 2008 in CodingProductivity
Comments are disabled for this post (read why), but if you have spotted an error or have additional info that you think should be in this post, feel free to contact me.

No comments:

Post a Comment