Rough Book

random musings of just another computer nerd

Tag: editors

Indenting XML and HTML from vim

vim has an awesome feature, using which you can pipe a range through an external command. This is pretty useful if you’re opening up an un-indented or poorly-indented XML or HTML file. If you want to indent your entire file, simply do the following:

:%!tidy -i -xml -q

The -i option tells tidy that it needs to indent the content, -xml tells tidy that the content is well-formed XML, and -q puts tidy into “quiet mode” where extraneous information is suppressed. You can also specify ranges like so:

:40, 74!tidy -i -xml -q

This indents content between lines 40 and 74 (both lines inclusive). You can also do:

:., .+50!tidy -i -xml -q

This indents the current line and the next 50 lines. You can also do the same for HTML:

:%!tidy -i -xml -q

You can of course, supply additional parameters to tidy to customize the indenting.

:%!tidy -i -q

vim: removing blank lines

This always seems to get me and I have to figure it out every time. Sometimes I have files with blank lines and I want to get rid of them. Here is how you do it in vim (one of the many ways, actually). First you want to get rid of whitespaces:

:%s/^\s\+$//

Note: I used a blockquote here because WordPress doesn’t like two forward slashes in the sourcecode tag.

Then, you have to do this:

:%!grep -v  "^$"

Of course, this only works in Linux, BSD, or any of the other Unices.

All original content on these pages is fingerprinted and certified by Digiprove
%d bloggers like this: