'Linux tool/editor that will nicely auto-format my code AFTER it's written

I'm looking for a tool or editor where I can paste in potentially messy code, be it HTML, Javascript, CSS, whatever, and properly take care of all indentation and spacing.

I use Geany but I don't see an option for this, surprisingly. I don't know if it's possible in vim or emacs. Any help would be appreciated.

Example HTML:

<!DOCTYPE html>
<html>
<body>
<ul>
<li>what a mess</li> 
</ul>
</body> 
</html>


Solution 1:[1]

There are some command line tools that can help (eg, 'indent' for C), but 'Edd's suggestion of Emacs and vi are probably the best that will work on all languages.

Solution 2:[2]

You could reformat/pretty-print the starting HTML and then work with the resulting file in the editor of your choice. For example on Linux you could use Tidy to clean up the HTML before editing it; you'd use a command like this,

tidy -indent ugly_file.html > nicer_file.html

(thank you to @shmit in this answer for the pointer to Tidy)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Wes Hardaker
Solution 2 Andrew Richards