Change all tags to lowercase in VIM

Sometimes, you’ve got a whole chunk of HTML code that needs cleaning up a bit before you can use it. This week I got some materials in .doc format that had to be put on a website. There was so much of it that simple cut and paste was not possible. It would have been be particularly tiresome to try to get all the tables to HTML. On the other hand, it’s quite easy to save a word document as an html file. However, the generated HTML code is not very clean and definitely doesn’t comply with XHTML standards (though it seems to me that the result is much better when you use OpenOffice rather than MS Office).

Now that you have a very messy piece of HTML code, making it look better would mean even more toil than the copy and paste process. This is where a little bit of Vim magic comes handy.

1. First we change all tags to lowercase:

:%s/<\/\?\zs\(\a\+\)\ze[ >]/\L\1/g

2. Note that this will change tag names only. To change tag attributes to lowercase as well, use this command:

:%s/\(<[^>]*\)\@<=\<\(\a*\)\ze=['"]/\L\2/g

3. The above two expressions were found using google. Now I needed something that would wipe out all attributes to get rid of unnecessary styles attached to tags so I came up with this search and replace expression:

:%s/<\([a-z]*\)\s\([^>]*\)/<\1/g

Leave a Reply