LearnTextmate: Re-Usable Command Basics

Treehouse
writes on September 11, 2009

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:

  1. the button pressed (numeric)
  2. 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!

0 Responses to “Textmate: Re-Usable Command Basics”

  1. Many cancer sufferers do not want to deal with any more doctors than necessary, and that is why they can enjoy the ability to Generic Online Viagra so much more than some men who would normally have no really big issue with going to a doctor to get a prescription.

  2. Thanks, I was just looking for this kind of command the other day. This will be very useful.

  3. 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/

  4. There’s also a way to create new commands using PHP: http://davidwalsh.name/textmate-php

Leave a Reply

You must be logged in to post a comment.

Learn to code with Treehouse

Start your 7 day free trial today and get access to hundreds of video courses in web development, design and business!

Learn more