Format Document script

Posted by jimtut on 2009-02-25 09:20

I found this script somewhere on this site:

if (komodo.view.scintilla) { komodo.view.scintilla.focus(); } // bug 67103
var formatter;
var language = komodo.view.language;
switch (language) {
    case 'Perl':
        formatter = 'perltidy -i=2 -pt=2 -l=0';
    case 'XML':
    case 'XUL':
    case 'XLST':
        formatter = 'tidy -q -xml -i -w 80';
    case 'HTML':
        formatter = 'tidy -q -asxhtml -i -w 80';
        alert("I don't know how to tidy " + language);
        return null

//save current cursor position
var currentPos = komodo.editor.currentPos;

try {
    // Save the file.  After the operation you can check what changes where made by
    // File -> Show Unsaved Changes

    // Group operations into a single undo

    // Select entire buffer & pipe it into formatter.
    Run_RunEncodedCommand(window, formatter + " {'insertOutput': True, 'operateOnSelection': True}");

     // Restore cursor.  It will be close to the where it started depending on how the text was modified.

    // On windows, when the output of a command is inserted into an edit buffer it has unix line ends.
catch (e) {
finally {
    // Must end undo action or may corrupt edit buffer

I love this macro - I use it all the time to format my Perl scripts (my primary language). However, it doesn't work for JavaScript. Komodo does have JS Beautify available in the Code -> Format Using menu. That's too many clicks for me, when I can have a macro that formats all of my other files.

I'd like to enhance the above macro to support JavaScript too, but I don't know what the command-line to call JS Beautify is.

I'd really appreciate anyone's help with this!


jimtut | Wed, 2009-02-25 10:24

I think I figured it out. Add this to the SWITCH statement:

case 'JavaScript':
  ko.views.manager.currentView.scimoz.replaceSel(js_beautify(ko.views.manager.currentView.scimoz.text, {indent_size: 2}));
  return null;

and copy in the entire js_beautify() function from the file beautify.js available from

WFM! Let me know if anyone else has any better ideas.

ActiveState Staff
Wed, 2009-02-25 11:13

Yes, JS Beautify does a good job for formatting JavaScript.

Komodo IDE 5 already comes with formatting commands, which uses this same JS Beautify script when formatting JavaScript.


teddyber | Fri, 2009-02-27 06:53

Are tidy and perltidy some software to install ? or are they available within Komodo Edit ?


by the way Javascript format works like a charm!

ActiveState Staff
Fri, 2009-02-27 11:21

Most formatters (such as perltidy, tidy) are separate standalone binary applications, which will need to be installed separately from Komodo.

Komodo Edit does come with "tidy", it is located under the "lib/support/html" sub-directory of the Komodo installation.


jimtut | Fri, 2009-02-27 07:40

I had to install perltidy. I don't think I was able to install it through ActiveState's PPM, though. I think I had to download from CPAN and install manually. I have a strange PPM setup, though, so try PPM first.

I don't have plain tidy on my system, but I don't work w/ the languages that reference tidy above (again, the original formatter script was posted somewhere here by someone else).

Google is your friend though...

peiro.alice | Fri, 2009-04-10 11:45

Hi everybody, i'm new to komodo. I tried this macro with Komodo Edit 5.1 but when i format an html file the javascript goes in exception and an empty alert with two empty button comes out. Closing the alert the komodo application doesn't respond anymore and i have to kill it.

Thank you for your attention


jimtut | Fri, 2009-04-10 11:53

I only have the IDE, not the Editor. I'm not sure if there could be a difference between the 2?

More likely you don't have "tidy" on your search path. See Todd's post above. Either modify the code to point to the included tidy, or copy it somewhere else in your Windows PATH. (I recommend the latter, so it's available outside of Komodo too.)

peiro.alice | Sat, 2009-04-11 05:33

Thank you jimut

Yes the error was due to the fact the i didn't put in my search path the tidy directory. Now the macro doesn't come out with anymore errors, but the problem is that if i apply it the file doesn't change even if a * appear in the tab near the file name meaning that the script modify the file but i don't see any changes. I've tried to apply the same tidy command of the js script from the prompt line and it works.

Thank you jimut for your help

peiro.alice | Sat, 2009-04-11 05:54

I solved the problem, i modify the path environment variable of the SO, but i didn't shutdown and start up XP so komodo didn't refresh is environments variables (Edit -> Preferences ... -> Environment). Modifying the path variable inside the komodo fix the problem.

I hope this can help someone



wyrdnexus | Mon, 2011-06-06 11:22

Okay, so there are a collection of comments here which point to the following:

  1. tidy is included with Komodo in /lib/support/html/
  2. Tidy needs to be in the search path
  3. OR the code needs to point directly to tidy.exe

I tried tried adding it to the User Environment Variables in [Edit::Preferences::Environment], but that didn't work. So i tried adding it directly to the windows PATH, and that didn't work.

There must be something simple I'm glossing over, but could someone at least point me in the right direction?

rudygotya | Wed, 2009-10-07 23:06

Edited some snippets from this forum. So if someone needs php/javascript indentation you can load the macro over here. (just use it on debian/ubuntu jaunty)

to use php functionality you have to install pear PHP Beautifier 1st


c_lshao@qti.qua... | Sun, 2013-08-18 10:53

I created a macro (by simply compy the above script) to format my perl script, but gives me error: komodo.document not defined. I have installed perl-tidy and put it on the environment Path. What does the error mean? How should I use this script? Do I miss something?

ActiveState Staff
Thu, 2013-08-22 12:46

It's an old script that was not compatible with the latest Komodo versions.

I've updated the script (replacing "komodo.document.language" with "komodo.view.language"), which should do the trick.