Create Commands in TextMate Using PHP

By  on  
TextMate Logo

TextMate is the best thing to enter my programming life since MooTools. TextMate is extremely flexible and contains a host of functions that make my programming time much more efficient. Snippets, commands, macros -- TextMate has it all. And when a command or snippet doesn't exist, write it yourself! In what language? Any language installed on your system! TextMate's so flexible that you can write commands in the language you feel most comfortable in.

The Textmate Command / PHP

The first thing we do is tell the command "textarea" that our command is going to be coded in PHP:

#!/usr/bin/env php

Next we read in the content of the current buffer:

<?php
	$content = file_get_contents('php://stdin');

Then...we add any PHP we want to modify the buffer contents. When the buffer content has been modified as desired, you echo out the content and the buffer's content is replaced:


//do whatever you want here
$content = str_replace('MooTools','MooTools FTW!',$content);

//echo out modified content
echo $content;

?>

Here's an image of the screen so you have a visual.

TextMate Bundle Editor

Have any TextMate commands you've created? Email them to me at david@davidwalsh.name and I'll share them on this blog in the future!

Recent Features

  • By
    How to Create a RetroPie on Raspberry Pi &#8211; Graphical Guide

    Today we get to play amazing games on our super powered game consoles, PCs, VR headsets, and even mobile devices.  While I enjoy playing new games these days, I do long for the retro gaming systems I had when I was a kid: the original Nintendo...

  • By
    I&#8217;m an Impostor

    This is the hardest thing I've ever had to write, much less admit to myself.  I've written resignation letters from jobs I've loved, I've ended relationships, I've failed at a host of tasks, and let myself down in my life.  All of those feelings were very...

Incredible Demos

  • By
    MooTools Image Preloading with Progress Bar

    The idea of image preloading has been around since the dawn of the internet. When we didn't have all the fancy stuff we use now, we were forced to use ugly mouseover images to show dynamism. I don't think you were declared an official...

  • By
    dwImageProtector Plugin for jQuery

    I've always been curious about the jQuery JavaScript library. jQuery has captured the hearts of web designers and developers everywhere and I've always wondered why. I've been told it's easy, which is probably why designers were so quick to adopt it NOT that designers...

Discussion

  1. EmEhRKay

    Great stuff. I too recently got into extending textmate, it seems pretty straight forward.

    One thing that I want to do is create a code completion command taht will allow you write a class name and it would offer up the public[static] methods with their arguments. Dion Almaer’s project functions command is what got me interested. This should be easy to accomplish with PHP5’s reflection objects. My only worry is caching of found results etc. I’ll share with the world if I ever figure it out.

  2. can i use Textmate in windows ?

  3. @haberler you can use e text editor as i’m using it.

  4. ray

    @haberler: textmate only mac os x ,in windows you can use intype or e-texteditor

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!