Muffinresearch Labs by Stuart Colville

Beautify JavaScript from TextMate or CLI | 6 Comments

Posted in Code, Software on 14th January 2008, 10:24 am by

As Einars has pointed out this functionality is part of the built-in JavaScript Bundle under “Reformat Document”. I’ll leave the rest of this post here as it does provide an example of how to set-up a command with an existing PHP script and if you’re not a TextMate user then this will work as a CLI script too and you can find more info on using PHP for CLI in TextMate here

I’ve just been reviewing some code that’s minned and I remembered looking at beautifier for JavaScript recently that was designed to convert minned code back to something more readable. The script I remembered was this one: http://elfz.laacz.lv/beautify/. The beautifier makes it really easy to dive into source to view code without having to resort to getting hold of the un-minned source. This is also especially useful when you don’t have access to the un-minned source.

By taking the PHP source and converting it to CLI, I was able to add a beautify JS command to TextMate.

Thanks very much to Einars "elfz" Lielmanis for making this script freely available it made it really easy to add this functionality to TextMate. My additions are highlighted.

#!/usr/bin/env php
<?php 

…See the main Beautify.js source here…

$fstat = fstat(STDIN);
$stdin = fread(STDIN,$fstat['size']);
echo js_beautify($stdin);

?>

The settings for the command are as follows:

Save:
Nothing
Input:
Selected Text or Document
Output:
Create New Document

With the above you can either build your own command or download this one pre-made: BeautifyJS.zip

Post Tools

GNU screen: open tab in current working directory|(1)

A nice trick for having screen open a new tab in the same directory as the one you’re currently in. To use it add it to your .screenrc

# Open new window in current dir.
bind c stuff "screen -X chdir \$PWD;screen^M"
bind ^c stuff "screen -X chdir \$PWD;screen^M"

Hat tip: mteckert on SuperUser.com

Ubuntu: add-apt-repository: command not found|(3)

When you’re using a minimal Ubuntu install if you find the ‘add-apt-repository’ command is missing (it’s useful for adding PPAs and other repositories), then simply run:

sudo apt-get install python-software-properties

Photos on Flickr

© Copyright 2004-12 Stuart Colville, all rights reserved. May contain traces of Muffin. Powered by WordPress. Hosting by Slicehost.com This page was baked in 0.560s.