I want to format HTML source files from the command line, one file at a time or in bulk.

Tool requirements

  1. Should be installable on Windows, Linux, and macOS.

  2. Must be capable of formatting in place, overwriting the current HTML file(s).

  3. Should allow setting the indent size to a single space. 1

  4. Should merely format the HTML code – not validate it.

  5. Must not make any false claims about the HTML code.

  6. The <head> and <body> tags should not be indented.

  7. The maximum number of line breaks to be preserved may be set to zero.

A tool I tried but discarded

I've tried prettier, but it erroneously reported that <form> is a self-closing tag in HTML. 2
This I cannot allow, which is why I discarded it.

An online tool

The online formatter I've found to best meet my requirements, is HTML Formatter.


1 The rationale for one-space indents is two-fold.

  • I want to keep down the file size.

  • When watching the file in a text editor, I want to use up as little horizontal space as possible.

2 I installed Node.js, and then globally installed the package prettier by running :

npm install --global prettier

Then I downloaded the file form-demo.html – in a zip file, so that the unformatted file can easily be restored. It has a <form> tag with a property action set to /search (instead of "/search"). I tried to format form-demo.html with prettier :

prettier form-demo.html

To that, prettier replied with a syntax error, referring to Closing elements that have implied end tags, but <form> is not a self-closing tag.

2 Answers 2


I want a tool that formats HTML files from the command line

– I recommend js-beautify, as suggested in this answer.
I prefer to install it globally:

npm install --global js-beautify

I typically want to format all *.html files in the current folder. 1

js-beautify *.html --type html --replace --indent-size 1 --max-preserve-newlines 0

The above fulfills all eight requirements of the question, including the three optional ones.


1 The command may be written shorter as :

js-beautify --type html -r -s 1 -m 0 *.html

This formats the HTML to my liking. Check out the Options to find your favorite settings.


Try html tidy.

Tidy is a console application for macOS, Linux, Windows, UNIX, and more. It corrects and cleans up HTML and XML documents by fixing markup errors .

It has many options and is available on many platforms, and online.

Installation on Windows and integration of its output into an editor such as Textpad was kind of tricky but it can be done . https://www.google.com/search?q=html+tidy

I am not sure about your requirement "indent 1 character" but I think this can be worked around by indenting with tabs, and then global substitution with of tab with 1 blank.

  • Thanks for answering my question. In my example HTML file, the title tag, <title>"prettier" says `<form>` is a self-closing tag.</title>, causes problems because I used a backtick and < instead of &lt;. In this respect, js-beautify is more forgiving than tidy, and for now, I prefer js-beautify to tidy. But for someone wanting to validate HTML, I can see how tidy is preferable to js-beautify. Screenshots: i.imgur.com/etHKMzD.png, i.imgur.com/RSMkfhT.png. Aug 28, 2023 at 14:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.