0

Sometimes I copy 'compressed' HTML from TEXTAREA an I get code for ex. like this:

<html><body><h1>header1</h1><p></p>
<p></p>
<div><p></p></div></body></html>

but GVim after command gg=G indent only lines:

<html><body><h1>header1</h1><p></p>
    <p></p>
    <div><p></p></div></body></html>

what I need is unfold(in a sense that tags are not in one line) all tags like this:

<html>

<body>
    <h1>header1</h1>
    <p></p>
    <p></p>
    <div>
        <p></p>
    </div>
</body>

</html>

I have installed Syntastic plugin and newest Tidy I try also :!tidy -mi -html -wrap 0 % but somehow nothing works.

2
  • The last Vim command (:!) starts a shell command, but it doesn't filter the buffer because it's not prefixed by a range. Does this work?: :%!tidy -mi -html -wrap 0 %, or maybe :%!tidy -mi -html -wrap 0 %:p:S?
    – user938271
    Oct 4, 2018 at 15:27
  • Command %!tidy -mi -html -wrap 0 works, but still gives a lot of comments at the top and bottom of the page like "line 1 column 29 - Warning: trimming empty <p>". And also trims empty html tags which I don't expect.
    – lluke
    Oct 4, 2018 at 15:39

1 Answer 1

1

A full vimscript solution could be to put each tag on its own line with a substitution and then rely on the built-in formatter:

:%s/></>\r</g

Gives:

<html>
<body>
<h1>header1</h1>
<p>
</p>
<p>
</p>
<div>
<p>
</p>
</div>
</body>
</html>

And then gg=G gives:

<html>
    <body>
        <h1>header1</h1>
        <p>
        </p>
        <p>
        </p>
        <div>
            <p>
            </p>
        </div>
    </body>
</html>

But I think that it would be better to use a specialized tool as you already tried. I think the problem in your command is that you used the -m̀ tag and misused %.

You want to put the % sign before !. This way it doesn't refers to the name of the currently edited file, it means that all the lines of the current buffer will be piped to the command written after !, and the standard output of the command will be put in the current buffer.

Now you don't need to give the -m tag to tidy since it will not modify an original file.

I did not test it but the following command should work properly:

:%!tidy -i -html -wrap 0

Your Answer

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

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