Muffinresearch Labs by Stuart Colville

Customising TextMate with commands | Comments (5)

Posted in Apple, Linux/Unix on 31st August 2006, 12:16 am by Stuart

One of the most useful things about TextMate is it’s ability to be customised through the use of command-line programs. On a mac due to it’s unix heritage there’s plenty of powerful command-line tools that you can make use of to add extra functionality to TextMate.

To add a new command to TextMate simply open the Bundle editor (bundles menu, Bundle Editor, Show Bundle Editor) and press the add button (+) and select “New Command”. This command will appear under the user’s bundle, you can move this later.

The command edtor window has several options these are:

  1. Save: This allows you to configure whether to save the file or the current project before running the command
  2. Command(s): This is where you enter your commands
  3. Input: The text that becomes the standard input to the command. This can either be a selection or the entire document (and selection)
  4. Output: This option is how the ouput from the command is handled. This can either be discarded, replace the selection, replace the document, insert as text, insert as snippet, Show as HTML, show as tooltip create a new document.
  5. Activation: How the command is triggerred, in each case I’ll leave this up to you.
  6. Scope: the scope of documents this should effect, e.g all js files etc.

So in other words what textmates does is to take some text and pipe it through a command. To demonstrate the power of what is possible I wiill give a couple of demonstrations of what is possible:

Example 1: Delete blank lines

To start with here’s a very simple example. We are going to use the sed command to remove all of the totally blank lines from a document.

Here’s the command configuration:

Save:
Nothing
Command:
sed /^$/d
Input:
Entire document
Output:
Replace Document
Activation:
I’d recommend using a keystroke of your choice
Scope:
Leave this blank

If you’ve not come across sed before, what it does is process a file line by line and carries out substitution based on regular expressions. In this example “/” is the delimiter. The regex part “^$” matches the beginning of a line and the end of a line (i.e a blank line) and lastly the “d” deletes the match.

WARNING: Please test the following examples on test files first to make sure you have set these up correctly, I’m not responsible if you accidentally minimise the internet

Example 2: Use JSMin via textmate

If you are into JavaScript and are looking to make your files as small as possible JSMin, written by Douglas Crockford is a command line utility that reduces the the size of JavaScript files. To be able to use JSMin as a command the first step is to compile it. On my intel mac this was as simple as downloading the C source code from www.crockford.com and using xcode to compile it. (Hint: Open a new project, look for command line utility, and select standard tool). Alternatively for the lazy amongst you, you can download the universal one I compiled here (22kb).

Once you have compiled JSMin you need to put it in a directory that’s within your environment path. In simple terms this is where the operating system will look when you run a command without a full path. Alternatively when specifying the command in textmate you can put the full path to JSMin. What I do is create a bin directory in my home directory and add that to my path.

For completeness here’s how you can add directories to your path. I’ve used the /Users/username/bin as an example but you can add a directory anywhere. If you want to check the existing path first type echo $PATH from terminal.

  1. Open for editing ~/.bashrc. If it doesn’t exist one will be created.
  2. Add a line that says export PATH=$PATH:$HOME/bin

Lastly you will need the mate command. Now this is a special command line utility that is optionally installed when you install TextMate. If you don’t have this (try running mate from terminal it should open TextMate). You will need to run this command from terminal:

ln -s /Applications/TextMate.app/Contents/Resources/mate /usr/bin/mate

Ok once this is all set. You can configure the command to run JSMin from textmate:

Save:
Nothing
Command:
require_cmd jsmin
require_cmd mate
newfile=${TM_FILEPATH/%.js/_min.js}
jsmin > $newfile
mate $newfile
Input:
Entire document
Output:
Discard
Activation:
I’d recommend using a keystroke of your choice
Scope:
source.js

Now to run through the command to explain it. First require_cmd command checks for the availability of a command. Next I create a file path based on the current document’s path by removing the “.js” and replacing it with “_min.js“.

Then I run the current document through JSMin (textmate automatically pipes the current document into the command) and then output to a new file manually with the filename I created above. Finally the mate command then opens the minimised file.

Thus with a quick keystroke you can now minimise open JavaScript files.

Example 3: JSMinimise Selected Project Files

No whilst the above is very useful we can extend this so that we can minimise more than one file at once. Textmate has a useful variable that contains all of the selected files in the project window. It’s possible to evaluate this into an array and loop through it like so:

Save:
Nothing
Command:
require_cmd jsmin
eval arr=("$TM_SELECTED_FILES")
for (( i = 0; i < ${#arr[@]}; i++ )); do
file=${arr[$i]}
newfile=${arr[$i]/%.js/_min.js}
jsmin < $file > $newfile
done
Input:
None
Output:
Discard
Activation:
I’d recommend using a keystroke of your choice
Scope:
Leave this blank

This is different in one main regard in that we define both the input and the output to JSMin manually e.g. jsmin < input > output

So there you have it. Feel free to add your favorite commands (not snippets) to the comments below and if you do, be sure to make use of <code> for your examples.

Post Tools

Comments: Add yours

1. On August 31st, 2006 at 4:47 pm Ed Eliot said:

Cool info, thanks. I’d love to be able to create a bundle command that pipes the file through scp though but then it would have to be able to prompt me for passwords and destination paths.

2. On August 31st, 2006 at 5:03 pm Stuart Colville said:

@Ed: This is really only scratching the tip of the iceberg as to what is possible with commands. You can set prompts for entering strings with apple script. See the textmate manual for more on what is possible.

3. On August 31st, 2006 at 6:59 pm Andy Hume said:

Just one more reason to actually find the time and open textmate for longer than three minutes.

I suffer from an acute case of argh-its-not-bbedit syndrome.

4. On August 31st, 2006 at 11:39 pm Stuart Colville said:

@Andy: I know what you mean, I felt the same when moving onto the mac from windows. I really missed the editor I was used to (notepad++). However, once you do give TextMate a little time it doesn’t take long to reap the rewards, it’s an incredibly powerful editor.

5. On January 4th, 2007 at 5:51 pm Chris Blow said:

I’m about 6 months late here, but thanks for this post. Just found it while looking for a sed syntax reminder, and now I remember how many more tools I am still not using in textmate. And ou can never have too many this-is-why-I-love-textmate posts.

@ed:

I solve the password problem with a public private keypair: http://hacks.oreilly.com/pub/h/66

Isn’t it lovely that you can arguably be *more* secure without using a password??







XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>



Standalone mac battery charger|(0)

Got a spare mac battery? I’ve often wondered why up until now no-one’s produced a standalone charger so that you can charge batteries without having to plug them into the mac. Fortunately Fastmac.com have produced a standalone charger that allows you to do just that. and it’s compatible with iBooks, Powerbooks, macbooks and Macbook Pros. It’s also 110/200v. Exactly what I was looking for!

Django Admin Ominigraffle Stencil|(0)

Colleague Alex Lee has created a nice stencil for omingraffle with the Django Admin UI components, perfect for wireframing customised admin screens. For more details and to download the stencil see Alex’s Blog csensedesign.co.uk

Photos on Flickr

© Copyright 2004-08 Stuart Colville, all rights reserved. May contain traces of Muffin. Powered by WordPress. Hosting by 1&1 This page was baked in 0.908s.