Beautify JavaScript from TextMate or CLI | Comments (6)
Posted in Code, Software on 14th January 2008, 10:24 am by Stuart
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

Nice Also, that must have been the longest block of code I’ve seen published for quite some time.
@Robert: Hehe – probably this is a good example of a need for me to implement a show/hide link on the code example! For now you can view it as text. At least that makes it a tad more readable.
Glad you found it useful =)
Michael Sheets of Textmate contacted me last november about inclusion of the beautifier to the standart javascript bundle; don’t know how often do they release Textmate, but it’s possible it’s in there already (can’t check it, I’m not a macperson (yet), but at least i see it in the svn, http://macromates.com/svn/Bundles/trunk/Bundles/JavaScript.tmbundle/Support/lib/).
@Einars: OK so I’ve just realised it’s in the JS menu under “reformat document.”
I’ll update the post accordingly!
[...] Beautify JavaScript from TextMate [...]
[...] Beautify JavaScript from TextMate or CLI [...]