Is there an HTML auto-indentation feature or plugin for Sublime Text (aka prettify/beautify/format)?

23

4

For example, something to change:

<section><article><h1></h1><p></p></article></section>

to:

<section>
  <article>
    <h1></h1>
    <p></p>
  </article>
</section>

...that works on full pages and snippets.

  • The built in Edit > Line > Reindent setting doesn't do this
  • The plugin HTMLTidy adds head and body tags, so it doesn't work for partials, it also has various other issues
  • The plugin Tag has various issues, enough that it essentially doesn't work
  • The plugin HTMLPrettify has various issues, requires node.js, and hasn't been updated in months
  • gist.github.com/mcdonc/3898894 claims to do it, but requires Emacs

(Notepad++ had auto-indent, Dreamweaver had Apply Source Formatting, Aptana has Format, etc.)

The Tag plugin handles inline tags incorrectly, for example using it on this snippet:

<p>foo<a>bar<span>baz<span>qux</span></span></a></p>

results in:

<p>
    foo
    <a>
        bar
        <span>
            baz
            <span>qux</span>
        </span>
    </a>
</p>

user110241

Posted 2013-03-11T14:52:18.737

Reputation: 343

I think this is the best working solution: http://jsbeautifier.org/ I've had the same requirement of format multiple inline tags. This one works really well. It create proper markup for sublime text as well which can close chunks. It's also available on CLI as https://www.npmjs.com/package/js-beautify

– Sai krishna Deep – 2016-12-26T15:39:57.257

What is the question? It's not clear from your post what the question is. – Brad Patton – 2013-03-11T14:56:18.023

Edited title to be interrogative. I'm trying to find some way to auto-indent HTML in Sublime Text. – user110241 – 2013-03-11T15:07:32.243

Also see Formatting HTML Code using Sublime Text 2.

– Karan – 2013-03-11T19:39:25.640

Thanks, I tried each solution in that thread, none of them work. I've listed the reasons why many of them don't work in this thread. – user110241 – 2013-03-12T14:10:46.183

Answers

18

The reindent doesn't always work for snippets. You can use the Tag plugin(install from package control). Then just press ctrl+shift+p, and type tag. You'll see the option Auto-Format tags on document. Select and press Enter. That'll do it.

Bibhas

Posted 2013-03-11T14:52:18.737

Reputation: 2 490

1As I mentioned in the original post, the Tag plugin is broken, most particularly in it's handling of inline tags, among other very bad bugs. The author has said he needs to "rewrite the whole thing". The reindent doesn't work for HTML really at all, it doesn't correct indentation; it just aligns tags to a certain indent level. – user110241 – 2013-03-17T02:19:35.413

well, Tag worked for me. I used your HTML snippet. It worked flawlessly. And reindent does work for HTML but under some certain conditions. – Bibhas – 2013-03-17T05:25:56.223

try using Tag's autoformat on this snippet: <p>foo<a>bar<span>baz<span>qux</span></span></a></p> to see what I mean. It also messes up the formatting of the opening HTML tag wrapped in IE conditional comments that is part of HTML5 Boilerplate (and comments in general). Try using the built-in Line > Reindent on this snippet: http://jsfiddle.net/y8xXj/ to see how it fails.

– user110241 – 2013-03-17T16:36:55.067

FYI - Just tried in Sublime Text 3 with an HTML snippet and it indeed does work! – Steven – 2013-09-03T17:57:23.170

Tried what? Neither Edit > Line > Reindent or the Tag plugin worked for me on this snippet in ST3: <p>foo<a>bar<span>baz<span>qux</span></span></a></p> – user110241 – 2013-09-05T13:40:18.597

2Same issue. I've used countless editors in the past, Coda, Sublime, Espresso, Chocolat, TextMate... but none ever have been able to flawlessly format text properly like Dreamweaver's Apply Source Formatting. To which people simply suggest Tidy or some other plugin that never work like Dreamweaver. I'm glad someone "Zooted" see's this issue. It's unfortunate there is still no solution. In Sublime Text 3 as Zooted Noted even with Tag it doesn't work properly (as in his above example). And the native Reindent feature is pretty worthless. – cchiera – 2013-10-26T21:06:30.827

6

Select All -> Edit menu -> Line -> Reindend will do. You could set a shortcut combination for the same use.

{ "keys": ["ctrl+shift+r"], "command": "reindent" , "args": { "single_line": false } }

Jasim Mahmood

Posted 2013-03-11T14:52:18.737

Reputation: 61

Works for me. Syntax set to HTML? – Manuel Arwed Schmidt – 2015-06-22T13:47:31.677

2Does not work for me. – Derek 朕會功夫 – 2016-02-29T20:07:41.813

Yeah, it does a pretty good job, but eventually started dropping, in my case <div> opening tags. – ruffin – 2016-08-20T14:24:15.493

1As I mentioned in the question, this does not work. Try it on the very first snippet I provided: <section><article><h1></h1><p></p></article></section> – user110241 – 2014-06-09T14:06:18.160

0

In order for the Reindent feature to work, you have to uncheck the option for "Indent Using Spaces":

View >> Indentation >> Indent Using Spaces

Once this option has been unchecked, you can select your text and Reindent:

Edit >> Line >> Reindent

This will work for HTML syntax as well as XML. Not tested it on any others.

Sidz

Posted 2013-03-11T14:52:18.737

Reputation: 1

I was actually already working with that unchecked. The command you mentioned does absolutely nothing to this snippet in ST3: <html><head></head><body><div><p>foobar<span>baz</span></p></div></body></html> – user110241 – 2016-06-18T18:07:59.880