12

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?

2
  • You want the function equivalent of a HTML beautifier in Emacs?
    – PythonNut
    Jul 28 '15 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. Jul 28 '15 at 18:35
20

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">
  <tbody>
    <tr class="c4">
      <td class="c33" colspan="1" rowspan="1">
        <p class="c6">
          <span class="c12 c11">Veld
          </span>
        </p>
      </td>
      <td class="c35" colspan="1" rowspan="1">
        <p class="c7">
          <span class="c12 c11">Waarde(n)
          </span>
        </p>
      </td>
      <td class="c26" colspan="1" rowspan="1">
        <p class="c19">
          <span class="c12 c11">Null
          </span>
        </p>
      </td>
      <td class="c18" colspan="1" rowspan="1">
        <p class="c6 c38">
          <span class="c12 c11">Validatie
          </span>
        </p>
      </td>;

[Source]

2
  • 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
    Jul 29 '15 at 7:42
  • sgml-pretty-print worked directly in Html+ mode :)
    – thomasa88
    Aug 1 at 8:38
2

The default HTML mode also works.

  1. C-x h - Select the whole buffer
  2. M-x indent-region (aka C-M-\)
2
  • That doesn't work if the newlines have been removed. Feb 29 '20 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
    Mar 4 '20 at 17:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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