11 September 2009
Textmate: Re-Usable Command Basics
At first glance, Textmate can appear to be nothing more than a bog-standard code editor, lacking the more advanced features of an IDE. However, a quick look under the hood reveals how its relationship with UNIX command line tools has allowed it to become a supremely powerful editor.
There are a whole host of available commands and bundles, but what if you need to extend existing ones or create your own? I’ll cover some of the basics which will help you get started…
For this example, I’m going to perform a simple append to end-of-line (EOL) operation on selected text using sed.
Using Filter Through Command
Textmate’s ‘Filter Through Command…’ (under Text) is our first port of call. This will allow us to send input data from Textmate to a command and then return the command’s output to the current document.

sed 's/$/,/'
We now end up with a comma at the end of each line of the selected text. The comma can of course be replaced with any string.
Re-using Commands
Textmate’s strength however lies in its bundles; essentially containers for re-usable blocks of code, templates and snippets. Rather than having to use the ‘Filter Through Command’ feature each time, we can replicate its behaviour in a stored command. We can also extend the original sed command by taking the content of the clipboard and appending this to the end of each selected line.
Under ‘Bundles > Bundle Editor > Show Bundle Editor’ we create a new command within the Text bundle.

#!/bin/bash
if [[ $(pbpaste|wc -l) -eq 0 ]]
then r=`pbpaste`
sed "s/$/$r/"
else sed "s/$/,/"
fi
The second line of our new command checks whether we have more than one line of data in the clipboard, if so we’ll default to simply appending a comma to the end of the selected lines (line 5). If the clipboard contains no more than one line, its contents are assigned to $r and placed into the sed command.
The input is ‘Selected Text’ or ‘Nothing’ and the output is ‘Replace Selected Text’. The command can be run by going to ‘Bundles > Text > Name of command’. We can also bind this command to a keyboard shortcut if we so wish.
Adding simple UI elements
As a final example, we can extend this command further by introducing a dialog box which prompts for the input of the string to be appended, rather than relying on the clipboard.
Create a new command under the Text bundle with the same input and output settings as above, enter the following as the command:
#!/bin/bash
r=$(CocoaDialog inputbox --title "String to be appended to EOL" \
--informative-text "Enter string:" \
--button1 "Okay" --button2 "Cancel")
[[ $(head -n1 <<<"$r") == "2" ]] && exit_discard
r=$(tail -n1 <<<"$r")
sed "s/$/$r/"
A CocoaDialog input box is called and we create two buttons, ‘Cancel’ and ‘Okay’, along with some assistive text. The dialog will return two lines of text:
- the button pressed (numeric)
- the string entered into the input
On line 6, we perform a check to see whether the ‘Cancel’ button was clicked (button number 2). If so we can exit, otherwise we retrieve the content of the second line returned by the dialog, assign this to $r and place it into our sed command.

Hopefully you’ll be inspired to modify and extend the examples for your own use. Please feel free to post these in the comments!
We're big fans of 


Dominik Porada
# September 11, 2009 - 3:28 pm
There’s also a way to create new commands using PHP: http://davidwalsh.name/textmate-php
Greg Annandale
# September 11, 2009 - 4:08 pm
Absolutely. Equally you can use Ruby, Perl, Python, Applescript and more.
As for PHP, mentioned in your link, just replace:
#!/bin/bash
with:
#!/usr/bin/env ruby
etc.
bananagreen999
# September 12, 2009 - 1:51 am
it’s really cool
Montana Flynn
# September 17, 2009 - 4:55 am
It really is. I am new to unix and have a hard time with it sometimes though. I prefer Coda. Thanks for the article and code.
Charles Roper
# September 12, 2009 - 11:26 pm
Great tutorial.
You’re not limited to doing this in Textmate, either: if you happen to be on Windows (or Linux, with a little extra compilation effort), then check out E Text Editor. E is compatible with Textmate’s bundles and so this command can be run as-is. I just tried it and it works great.
Windows obviously doesn’t have all of the UNIX command-line tools, but E takes the innovative approch of bundling Cygwin, so you get all that UNIX-y goodness right out of the box, plus you can use Windows native tools such as Powershell as well, if you feel like it. It’s pretty cool. Check it out:
http://www.e-texteditor.com/
Greg Annandale
# September 16, 2009 - 10:34 am
Thanks Charles, yep I should have also mentioned E. Have been using it for the odd bit of dev on Windows machines and the Cygwin integration works perfectly well.
Kaelig
# November 26, 2009 - 10:28 am
Thanks, I was just looking for this kind of command the other day. This will be very useful.