Book Image

TextMate How-To

By : Chris Mears, Christopher J Mears
Book Image

TextMate How-To

By: Chris Mears, Christopher J Mears

Overview of this book

Not all text editors are created equal. TextMate was created with versatility and power to the developer. Whether you want to organize your files in projects, utilize the efficiency of code completion, take advantage of the thousands of publicly available bundles, or gain insight using powerful regular expression searching, TextMate is an essential and easy-to-use tool you need in your development arsenal. "TextMate How-To" is a practical guide filled with conventional recipes on using one of the most popular text editors on the OS X platform. It is a great resource for anyone who wants to learn how to code more efficiently and effectively, no matter what programming or scripting language you use. This book will guide the reader through using TextMate for practical purposes. It will start with configuring the application, then cover navigation and editing of files, and finish with bundles and advanced features. Throughout the book, the reader will quickly master the key features of the application through easy to follow tasks. Using this book, the reader will learn the most common text editing and coding tasks including navigation through the document and searching of text. The reader will then learn about working with and navigating between files and projects as well as utilizing bundles to greatly speed up development. Finally, explore the ability to use shell commands and macros to increase productivity. With just a few hours, "TextMate How-To" will teach the reader everything necessary to hit the ground running with this powerful text editor.
Table of Contents (7 chapters)

Executing shell commands (Become an expert)

This task will cover how to execute shell commands directly from the current document.

How to do it...

The steps to execute shell commands directly from the current document are as follows:

  1. Add a new command into the Bundle Editor window (Control + Option + Command + B) by clicking on the plus (+) icon and selecting New Command:

  2. Fill in the Command(s) text field with the commands you wish to execute, along with the Input, Output, and Activation options. You can also select this option via the Bundles menu.

  3. For the Command(s) text field, enter curl -sSd "css=$TM_SELECTED_TEXT" \ for the Input option, set Selected Text or Document; for the Output option, set Replace Selected Text; and for the Activation option, set Key Equivalent and Control + Option + Command + U, as shown in the following screenshot:

  4. Execute the command by selecting the text you wish to input. This is shown in the following screenshot:

  5. And then use the chosen activation action (the keyboard shortcut Control + Option + Command + U in this example):

How it works...

The example in this task uses Jeffery Way's Prefixr (, which is a fantastic utility to make the CSS cross-browser compatible quickly and easily.

From the previous example, when you use the keyboard shortcut, the selected text (the input) will be sent to Prefixr via cURL (, which is a command-line utility that transfers data from a variety of protocols, such as HTTP. The returned data (the output) will then replace the selected text.

There's more...

Let's also look at how to execute commands from the document editor. Plus, we'll find out how to set environment variables to aid in development.

Executing lines and inserting results

Another way to execute commands in TextMate is to execute the current line and return the results. This can be done from the menu (select Text | Execute Selection Inserting Result) or the keyboard shortcut Control + R:

More on environment variables

In this section's example, we made use of the TextMate environment variable $TM_SELECTED_TEXT. There are a number of other similar environment variables that can be used for commands and more complex bundle operations. A full list of the variables can be found on Macromate's TextMate wiki:

You may also wish to utilize static shell variables (select Preferences | Advanced | Shell Variables), which you can use to declare usernames and passwords to your MySQL database or services such as Shopify ( The Shell Variables window is shown in the following screenshot:

You can also specify shell variables at the project level by clicking on the information icon in the Project Drawer menu, as shown in the following screenshot:

The Project Information window is shown in the following screenshot, where usernames and passwords of services such as MySQL can be set: