For example, I have the following bunch of characters (HTML): http://pastebin.com/pwTPp8wd

I thought everyone stumbled it upon a processed file, especially when someone would speed up the processing of large HTML/Javascript/whatever file. Then there must be a way to automate the indentation, without applying manual. However, I couldn't find anything about force indentation on a optimized file.

Any thoughts?

  • You want the function equivalent of a HTML beautifier in Emacs?
    – PythonNut
    Commented Jul 28, 2015 at 16:26
  • A quick googling gave me this: github.com/yasuyk/web-beautify Do you want to try it out? The solution is not completely in emacs though. Commented Jul 28, 2015 at 18:35

2 Answers 2


You can use the built-in sgml-mode for this.

  1. M-x sgml-mode - Enable the sgml-mode major mode so that sgml-pretty-print command auto-loads.
  2. C-x h - Select the whole buffer.
  3. M-x sgml-pretty-print

To save space, below is only a part of the formatted text that I get on doing the above on your sample text in the question:

<table border="4px" cellpadding="0" cellspacing="0" class="c31 c42">
    <tr class="c4">
      <td class="c33" colspan="1" rowspan="1">
        <p class="c6">
          <span class="c12 c11">Veld
      <td class="c35" colspan="1" rowspan="1">
        <p class="c7">
          <span class="c12 c11">Waarde(n)
      <td class="c26" colspan="1" rowspan="1">
        <p class="c19">
          <span class="c12 c11">Null
      <td class="c18" colspan="1" rowspan="1">
        <p class="c6 c38">
          <span class="c12 c11">Validatie


  • Thanks for your answer, nice that this is built-in in Emacs. However, I found some minor faulty indentation, but overall, the M-x sgml-pretty-print is good enough. Thanks for the help.
    – ReneFroger
    Commented Jul 29, 2015 at 7:42
  • sgml-pretty-print worked directly in Html+ mode :)
    – thomasa88
    Commented Aug 1, 2021 at 8:38

The default HTML mode also works.

  1. C-x h - Select the whole buffer
  2. M-x indent-region (aka C-M-\)
  • That doesn't work if the newlines have been removed. Commented Feb 29, 2020 at 18:53
  • Agreed, that is a problem. Another thing: HTML is lax, a page with errors can often still load in a browser. I removed a </p> and </td>, but indent-region still worked.
    – rleir
    Commented Mar 4, 2020 at 17:01

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.