Curated by Carsonified

Learn with Treehouse

Accessibility, CSS3, Design, Django, HTML & CSS, HTML5, JavaScript, jQuery, NoSQL, PHP, Responsive Web Design, Ruby, Ruby on Rails, Tools, UX, Version Control, WordPress, iOS and more

Article 8

Textmate: Re-Usable Command Basics

By

11 September 2009 | Category: Code

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.

tm_filter_command_sed

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.

tm_bundle_edit_eol

#!/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.

tm_append_eol_dialog

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

Follow @thinkvitamin on Twitter Please check out Treehouse

Other Posts You Might Find Interesting

  • Sorry - No Related Posts Found

Comments

  • http://dominikporada.com Dominik Porada

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

  • http://twitter.com/greg_a Greg Annandale

    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.

  • http://www.watchthevampirediaries.org bananagreen999

    it’s really cool

  • http://twitter.com/charlesroper Charles Roper

    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/

  • http://twitter.com/greg_a Greg Annandale

    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.

  • http://blog.complimedia.com Montana Flynn

    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.

  • http://www.kaelig.fr Kaelig

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

  • http://www.topviagra.com viagra online

    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.

Learn Web Design!

Ads Via The Deck

Think Vitamin Radio
Episode #34: Amazon Fire and Responsive Roundtable

Check out our bi-weekly radio show. Covering the hot topics on the web.

Audio clip: Adobe Flash Player (version 9 or above) is required to play this audio clip. Download the latest version here. You also need to have JavaScript enabled in your browser.

Download Podcast as mp3

Advisory Board

The Think Vitamin Advisory Board in place make sure that you receive the best content possible.