Take the 2-minute tour ×

I'm looking for a tool that can pretty-print (AKA tidy or beautify) source code in as many languages as possible. Those I'm particularly keen on include:

  • Java
  • JSP
  • HTML
  • JavaScript
  • SQL
  • JSON
  • XML

Ideally, the tool should be able to update source files in-place and be able to format more than a single file at-a-time. It would be great if it could format files containing multiple languages (e.g. a JSP containing HTML, Java, and JavaScript source code), but that's probably asking for a bit much.

I've already found a commercial tool that seems to cover a lot of languages, but a free one would be even better :)

BTW, I know there is a pretty printer available for most languages, but what I'm looking for is a "one-stop shop".

Cheers, Don

share|improve this question
Free?!, Polystyle is only $1.998 per supported language! ;) –  Aardvark Oct 30 '08 at 20:48
Too rich for my blood :) –  Dónal Oct 30 '08 at 21:05
I tried it out on a file containing JSP code and it seemed to work fine. –  Dónal Oct 31 '08 at 2:43

5 Answers 5

I use Vim to do this all the time. It doesn't handle HTML very well, but it does the others. It's easy to use the Vim commands to automate running it on a number of files.

share|improve this answer
I use gvim too, though I'm no ninja. How exactly do you get it format the source code, and how do you set the number of tabs/spaces to use for indents? –  Dónal Oct 30 '08 at 20:36
Indent spacing is set with: :set ts=4 sw=4 (to set tabstops to 4 and shiftwidth to 4). Include in an .vimrc file or an environment variable. –  Jonathan Leffler Oct 30 '08 at 21:28
Thanks, any idea how you do pretty printing? –  Dónal Oct 31 '08 at 2:44
the autoformatting is done by pressing the = key in command mode. Highlight the entire buffer with ggVG then tap = (you may need to do it twice). You can also do it on smaller selections of the buffer, but sometimes it assumes your selection should start at indent level 0. –  rmeador Oct 31 '08 at 16:46

I use UltraEdit and find it's generally pretty good. It's not free, but it's also not that expensive.

share|improve this answer

Wikipedia has a good-sized list. I'm interested to see what consensus might emerge here, though.

Several handle multiple languages; e.g.

PrettyPrinter, (an Open Source beautifier for every programming language)

UniversalIndentGUI (A graphical user interface for any pretty printer with live preview for the formatting settings)

prettyprinter.de, (an online beautifier for PHP, Java, C++, C, Perl, JavaScript, CSS)

share|improve this answer
What I'm looking for is one pretty-printer that can do multiple languages, not one for each language. –  Dónal Oct 30 '08 at 20:48
There are several that handle multiple languages. –  dkretz Oct 30 '08 at 20:52

Emacs is a personal favorite of mine. There are add-ons that allow for almost every language you can thing of.

share|improve this answer

For Haskell, I have this in ~/bin/pp.hs:

#!/usr/bin/env runhaskell
module Main (main) where
import Language.Haskell.Parser
import Language.Haskell.Pretty
import System.Environment
pp f = case parseModule f
        of ParseOk m -> prettyPrint m
           a -> show a
main = do args <- getArgs
          mapM_ (>>= putStrLn . pp) $
            if null args then [getContents] else map readFile args

In Vim, :set equalprg=~/bin/pp.hs, select a region, hit =, and boom, it gets prettified.

Okay, it's not very general. But I figure it has a small chance of helping somebody if I add it here.

share|improve this answer

Your Answer


