TextMate 1.5 Review
Volume Number: 22 (2006)
Issue Number: 4
Column Tag: Review
TextMate 1.5 Review
by Joe Zobkiw
Macromates, the creators of TextMate, calls it "the missing editor" with a nod to "The Missing Manuals" made popular by David Pogue. I would agree with their other tag line as well, that TextMate truly is "code and markup brought to the 21st century." But what exactly is all the buzz about this text editor that popped out of nowhere to take Ruby on Rails developers by storm, and is now spreading like wildfire to other programmers, hackers and geeks alike? Let's take a quick look at how I discovered it and then delve into the details that make TextMate so enticing.
Introduction
I first noticed TextMate in the early Fall of 2005. I found it by watching a Ruby on Rails screencast which seemed to be hard to follow as the developer was so quickly making text snippets appear on the screen and typing over (and through) them. Although it took some time and some Googling, I finally figured out how text editor was being used. I visited the TextMate web site, downloaded the 30-day trial version and started playing with it. Initially, I was excited with having found something new and after reading the hype on the web site, I felt like this might be something that would reinvigorate me. You know how, sometimes, you just need a new application for no particular reason. However, after a day or two of playing with it (without reading the manual), I decided it wasn't for me - "too much new stuff to learn," I thought. I was primarily using PHP, XHTML and SQL, and although it supported those languages, all the talk was Ruby Ruby, Ruby. I felt left out.
Fast-forward to the winter of 2005, when I came across TextMate again. This time I noticed it had grown up a bit. In fact, version 1.5 had just been released, so I decided to give it another try. This time I read the online manual and watched some of the new screencasts to see how others were using it. I was extremely impressed with the power behind this young text editor. So much so in fact, that I paid for it this time, and have been using it daily ever since. By leveraging the power of plug-in-like modules called bundles and interfacing with the power of the UNIX command line, Allan Odgaard had created a very capable application that could increase productivity all the while making it fun to use. Yeah, that's right, it's simply fun to use.
Figure 1. TextMate in action editing HTML code
TextMate is built on bundles. In version 1.5 there are over 30 bundles that define how TextMate behaves for any particular file type or language. Each bundle contains any number of 7 different types of items. These items consist of commands, drag commands, macros, snippets, languages, preferences and templates. All bundles (and their subsequent items) are completely customizable. You can edit what's already there, create your own or download those created by other users, essentially making TextMate infinitely extensible. Mind you, creating a TextMate bundle is not for the faint hearted, but, it is for geeks. Some of the default bundles include those that help you work with Apache, C, CSS, HTML, Java, JavaScript, LaTeX, Markdown, Objective-C, Perl, PHP, PHPDoc, Python, Rails, Ruby, SQL, Subversion, Textile, XML, and YAML. There are also more generic bundles that handle non-language-specific tasks such as Diff, Source, UNIX Shell and Web Searches. Other bundle items assist with unit testing in both Java and Ruby.
Bundle items are triggered in various ways throughout TextMate. Languages are automatically chosen based on the file suffix of the currently active file and the location of the insertion point within the file, but more on this later. Drag commands are triggered when a file is dragged and dropped into an editor window. We'll see an example of this as well. Templates allow you to create new documents based on their contents. Commands, macros, and snippets are triggered either from the gear menu (See Figure 2) at the bottom of any editor window or by typing text and/or typing special trigger keys. For example, in a PHP code block, typing the word foreach and pressing the tab key spits out an entire foreach ($variable as $key => $value) { } block of code. Even more amazing, is that, the word variable is pre-selected allowing you to type the real name of your variable over it. TextMate calls this functionality placeholders. Pressing tab again selects key allowing the same type-over capability. This functionality continues for value and the middle of the { } characters. The bottom line is that you save over a dozen keystrokes using this handy shortcut. TextMate is filled with hundreds of these little enhancements to your personal workflow.
Figure 2. The gear menu showing text commands, macros and snippets.
Let's take a look at a bundle in more detail to see what TextMate has to offer.
The HTML Bundle
The HTML bundle (see Figure 3) is a good example to discuss because many readers are familiar with HTML and the bundle itself contains at least one of each aforementioned bundle item. This will give you a good idea of not only what is possible with TextMate today, but, how you can extend it for your own needs. However, note that when you download TextMate and try it for yourself (and you will if you haven't already) you can easily explore other non-HTML bundles that make sense for the work you do.
Figure 3. The bundle editor showing some of the contents of the HTML bundle
Commands
Commands in TextMate are scripts interpreted by bash or whatever interpreter you specify including Perl, PHP or Ruby. The scripts can take the entire current document, the selected text, or, nothing as input. The output can replace the current document, replace the selected text, be shown as a tool tip or a variety of other options including being discarded altogether. Commands can also optionally save the document before executing. As stated earlier you can also set a key equivalent for any command or a tab trigger (like our foreach example). Commands can also be programmed to request user input much like AppleScript uses dialog boxes. Command progress can also be displayed using percentage or barbershop pole progress indicators.
Commands are very powerful in that, they can execute any script that you can call from the command line Terminal application, alongside complete interaction with the data in TextMate. You can perform tasks as simple as displaying (or inserting) the uptime in a TextMate document, or, as complex as grep-ing the selected line of text for a zip code, accessing an outside data source (such as a web site) to look up the name of the city and state for that zip code. The rest of the line of text can be verified, making any necessary corrections automatically. The possibilities are practically limitless, and, in essence make TextMate a very open application. Other commands built into TextMate include calculator functionality in the Math bundle, as well as software version control in the Subversion bundle. You can even control Apple's Xcode from TextMate with commands in the Xcode bundle.
Looking at a specific example, in Figure 3 we see the Validate Syntax command, which can be triggered by typing control-shift-V or by selecting the item from the HTML submenu of the gear menu. You can see that this command consists of a call to the command line application curl. By extracting the text of the entire document and passing it to the w3.org website via curl and then displaying the HTML results in a separate window, TextMate affords us a simple way (meaning no excuses) to check the syntax of our HTML documents quickly.
You may think that with so many commands and so many possible key equivalents that you will have trouble keeping it all, straight. Well, the good news is that TextMate is very intelligent when it comes to managing scope. As we'll see in the definition of language items below, key equivalents and tab triggers only apply in the specific scope that the bundle determines. Therefore, the PHP foreach example we showed earlier will not work if you are in a SQL file, for instance. Given this fact, you can re-purpose key equivalents as appropriate. A perfect example is control-H. TextMate implements control-H in a variety of bundles to look up documentation for the selected text. In HTML, control-H looks up the selected element on w3.org When triggered in a Perl file the selected text is looked up on perl.org No matter where the documentation comes from, TextMate displays it in a separate window saving you a trip to your browser. However, in the case where you do use the same key equivalent for multiple commands, TextMate merely pops up a menu with all the choices through which you can easily scroll, and make the proper selection.
Drag Commands
Drag commands are very similar to regular commands with the exception that the method that triggers them is dropping a particular file type into a TextMate document. For example, the HTML bundle contains a drag command named Insert Image With Dimensions. It is set to trigger whenever the user drops a png, jpg or gif file into the document. When this occurs, the drag command is executed and inserts a complete img tag into the document with src, height, width and alt tag information entered automatically. One of the many attentions to detail seen throughout TextMate is apparent in this example, in that, when you drag a file named figure_1.png the drag command creates an alt tag with the value "Figure 1." TextMate automatically converts the underscore to a space and properly capitalizes the text.
This particular drag command works by calling the sips command line tool built into Mac OS X to extract the height and width of the dragged file and then inserts the completed img tag into the text file at the location of the drop. One thing you will notice by looking at the actual code behind many of TextMate's commands is that it makes use of other command line tools (such as Perl, PHP and Ruby) to parse text and perform other actions pertinent to the task at hand. TextMate makes use of all of the available power under the hood to make your work easier.
Macros
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
To insert the previous paragraph of text I merely typed the word lorem and then pressed the tab key. This is a very simple macro but the macro capability in TextMate is much more than this. TextMate allows you to record a macro by selecting Start Macro Recording from the Automation menu. Once macro recording has begun, it will capture all text editing, usage of the Find menu command, running other commands as discussed above, inserting snippets, etc. The macro will then appear in the bundle editor and allow you to attach it to whichever bundle is appropriate, as well as, set key equivalents, triggers, and other settings. Once a macro is recorded it cannot be edited.
Snippets
Normally when you think of a snippet, you probably think of a static piece of text that you can quickly insert in a document. Many times this is done using copy and paste, copying the snippet from your repository of said snippets and pasting it into your working document. As you may expect by now, TextMate takes this expected functionality and spins it around a few dozen times with little regard for the fact that it may have just eaten.
A snippet in TextMate can be static text but it can also include code that can be run when the snippet is inserted. The snippet can also contain variables, such as the selected text at the time of the insertion. This essentially gives snippets the ability to wrap the selection in whatever other text you so desire. Snippets can also contain placeholders for missing information which you can then tab through and replace, such as the PHP foreach example we discussed earlier. The snippet can also perform transformations on the data you enter within the placeholder. These transformations can take the form of regular expressions, calls to command line utilities or execution of shell scripts.
One particularly good example of using placeholders and transformations is in LaTeX. Consider this, instead of typing the line of text \section{Introduction to Music}\label{sec:introduction_to_music}, TextMate allows me to do the following: Type the word sec and press the tab key. This inserts the text \section{section name}\label{sec:section_name} replacing where I typed sec and highlights the words section name. I can then type over those words to set my section name, in this case Introduction to Music. All the while I am typing the mixed-case text Introduction to Music, TextMate is inserting the same text in all lowercase underscored format where the section_name text appeared originally. Pressing the tab key selects the lowercase underscored version allowing me to type over that if I so desire, and with a final tab press moves the insertion point to the next line. The bottom line being that TextMate allowed me to type in 27 characters that in a normal text editor would have taken me 65 characters.
The HTML bundle contains numerous snippets that handle everything from inserting head, title, meta and script tags to tables, forms, anchors, and embedding movies. For instance, typing the word movie and pressing tab, inserts 10 lines of code with the src of the movie pre-selected for you to replace if needed. Tabbing allows you to alter the various parameters of the embed tag quickly and easily. Once you create a few forms using this abbreviated entry method (typing and tabbing) you will be forever changed and save countless keystrokes. Not only does it make the typing itself faster and more accurate, but, also saves you from having to recall potentially obscure attribute names within a tag since they are already included in the snippet.
Languages
Languages, or language grammars, are the items in a bundle that help TextMate decide what, within a block of text is a keyword, comment, variable, or string. The information gleaned from this item allows for syntax highlighting and to make the editor smart about which context the insertion point is located. As mentioned earlier, this is how TextMate knows when you type control-H, if you are looking up an HTML tag or a PHP keyword. Language grammars are used to parse documents and assign the proper scope to any particular position within the text. Everywhere else throughout TextMate, this information is used to narrow down functionality based on a particular scope. Although delving much deeper into language grammars, is well beyond the scope (no pun intended) of this review, suffice it to say that TextMate offers you the capability to implement any language that you may need that isn't already packaged with the product. For instance, if you have a custom text-based file format you can create your own grammar for it and further create commands, macros, snippets, templates, and more. The language grammar item is the crux of the bundle.
Preferences
Preference items in a bundle are useful should you need to manipulate functionality based on scope within a document. For example, should you need to alter matching pairs of characters (such as {and }) or patterns that require text to be indented or outdented, based on where you begin typing (such as for text within a function declaration), this is the place to do it. Mind you, TextMate doesn't give you checkboxes and pop-up menus to set these options. It's all typing in text as in Figure 3, but the power is there.
Templates
Templates are shells of text that allow you to quickly create a new file based on the template, no surprises here. A bundle can contain any number of templates, for example, the HTML bundle contains templates for XHTML 1.1, XHTML 1.0 Strict, XHTML 1.0 Transitional, and XHTML 1.0 Frameset documents.
Other Features
Beyond the realm of bundles TextMate has dozens of other features. Some are staples, while some are unique extras. Let's take a look at some of the highlights.
Projects and Tabs
TextMate supports the concept of a project. The easiest way to work with projects is to drag an entire folder over the TextMate icon in your dock and TextMate will open a tabbed window with a project drawer open to the side. The project drawer displays the file hierarchy of your project folder and as you click on files in the drawer, opens them as tabs to be edited. The tabs can also be rearranged at will.
Color and Style Themes
TextMate supports syntax highlighting, including a very intricate engine that allows you to choose a different font style for each scope selector. In reality this can make for some very busy color schemes, but, if used for good and not evil you can come up with some pleasing combinations as well. TextMate ships with a dozen or more themes and users submit themes to the Wiki on a regular basis. However, be warned, all of the time you save, actually using TextMate can be gobbled up by tweaking the settings in this preference.
Figure 4. The theme editor Folding
Folding, the ability to collapse portions of your document, is also available as can be seen in Figure 1. Note on line 5 that the head tag is collapsed so you cannot see lines 6-10. However, hovering your mouse cursor over the ellipsis icon will display the collapsed contents in a tool tip. Folding can come in handy if you have sections of code, that do not need your attention at the moment, and, getting rid of them makes for less scrolling and easier editing. TextMate even remembers foldings after a document is closed.
Editor Pop-up Menus
At the bottom of the editor window are a series of pop-up menus including the gear menu, which gives access to commands, macros, and snippets, as well as, a function pop-up that displays a parsed list of entities in the current document. I say entities because for source code this might include classes, functions, methods or procedures, but, for HTML it may more likely contain element ids of divs and form inputs.
Smart Typing
TextMate has a Smart typing preference that can be enabled or disabled. This performs such tasks as balancing characters while typing, by inserting matching closing characters. That is, if you type a {it will type the matching } for you and let you continue typing the text in between. Matching character pairs themselves, are configurable via the bundle preference items for the current context.
Web Preview
TextMate has a built-in web preview window that uses WebKit built into OS X. This is no big deal really, and normally I wouldn't find myself using this feature, since I always have Safari and/or Firefox open when working on web sites. However, it does have one feature that makes it very nice. The refresh after change setting allows you to see changes to your document in the web preview window as you make them. That is, as you type HTML code, the changes are immediately displayed, as they would appear in a web browser. This, saves you having to change to another application and press the reload button while working on a table, stylesheet or form, and can be a big time saver.
Filter Through Command
Another very powerful feature, is the Filter Through Command menu item (see Figure 5). This allows you to type in any command that can be executed in the Terminal application, and filter the selection or the entire document through that command. The output can be displayed back in the document, as a tool tip, in a web preview window or in a new document. Need to sort a selection of columns? Simply run the selection through the sort command. Need to add line numbers to the text itself? Use the cat -n command. Want to encrypt the text as base64? Use the openssl base64 command. The possibilities here, are endless.
Figure 5. The Filter Through Command dialog box
Miscellaneous Editing Features
TextMate contains all sorts of nice editing features including columnar editing, spell checking, the ability to automatically indent pasted text, overwrite mode, and clipboard history, allowing you to copy multiple items in a row, and then paste them in another document in LIFO-style. Also, if you happen to be on the command line manipulating some files, and have a quick need to edit something in TextMate, simply type mate filename, and the TextMate command line helper application will open the file in TextMate for editing.
But What's Wrong With It?
TextMate is not perfect, although it might seem like it. It is young, and has some growing up to do. There are a handful of things that I personally have trouble with, including the fact, that it's a little slow with really large files, and it takes at least a split second for syntax coloring to take effect, whenever you open any size file. TextMate only supports tabbed windows when using what it calls a project. This makes using tabbed windows, when editing remote files via SFTP impossible, unless you mount the remote server as WebDAV or some such. But then, that ends up being too slow for real work in many cases. It doesn't support splitting windows. Some of the language definitions still need some work (XML, for example) but since these are user-editable it's hard to complain about something like that. Sometimes the smart typing is not my kind of smart, so I've disabled it in my copy. Also, every once in awhile I'll come across a series of text that doesn't quite parse properly, usually when there are nested comments or comments in different languages in the same file. However, in the two distinct cases I can recall, it was a matter of changing the comment style from /* */ to // (for example in PHP), and it fixed it even though /* */ worked elsewhere. With all of the things that TextMate does right, I'm willing to live with the shortcomings because the developers are very responsive to feedback, and I expect in due time these issues will be addressed.
Conclusion
It may take some practice to become proficient, using TextMate. Having said that, it is also a fresh look at the future of text editing. TextMate takes text editing to a new level. The keyboard-oriented power of editors, like vi and emacs, with a Mac OS X graphical user interface, allows you to find new ways to optimize your work habits, almost every time you use it. If you have trouble along the way, there is a growing TextMate community available for support on the web, via email and IRC in the ##textmate conference room. Also, the online manual contains many details on how to make the most out of all of the features reviewed in this article, plus more. If you value your time, and editing text is crucial to your work you should give TextMate a look. At 39 Euros, and a free 30-day trial you have little to lose, except a few thousand keystrokes. You can learn more about TextMate at http://www.macromates.com/
Joe Zobkiw is a software developer who has been using the Macintosh since 1986. He has written two books and numerous articles on Macintosh programming. He can be reached via zobkiw.com