evfwcqcg
evfwcqcg

Reputation: 16365

How to format html onliner?

Say I have a line of html code,

<div><p><span>foo</span></p></div>

I want to convert it to something like

<div>
  <p>
    <span>foo</span>
  </p>
</div>

Is there any plugins or native solution to do this?

Upvotes: 0

Views: 92

Answers (3)

Andy Ray
Andy Ray

Reputation: 32076

:%s/></>\r</gEnterggShift-vShift-g=Enter

(you will nee to :set ft=html if you haven't already)

When I do the above, I get

<div>
    <p>
    <span>foo</span>
    </p>
</div>

Unfortunately VIM does really poorly with most front-end related indenting (html, javascript). It's a common complaint, and will probably never have a good solution despite all of vim's customizable power.

Upvotes: 1

Kent
Kent

Reputation: 195249

if you work on linux box, usually xmllint was already installed. try this command:

:%!xmllint --format -

this will change the text to what you wanted. but there is <?xml version="1.0"?> at the beginning. don't know if it is ok for you.

if you want to remove, after executing the command, the cursor will be at line, type dd to remove. or you can write it in a mapping.

Upvotes: 1

romainl
romainl

Reputation: 196886

Use the tidy program (or its HTML5 sibling).

That's what it has been designed for and that's what regular expressions are not designed for.

Upvotes: 3

Related Questions