VIM: Changing all HTML tags to lowercase

One part of converting from HTML to XHTML is changing all the tags to lowercase. If you open your HTML file in Vim, this task may be done with this piece of Vim magic:

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

Note that this will change tag names only. To change tag attributes to lowercase as well (multiple attributes supported), use this command:

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

This should work as well:

:%s/<\([^>]*\)>/<\L\1>/g

This is exactly what I thought of to start with. It is a nice, simple, easy regular expression to accomplish 'almost' the same task. But, it is not quite equivalent. The two regular expressions given in the tip would replace:


1
&lt;TAG ATTR='VAL'&gt;
with
1
&lt;tag attr='VAL'&gt;

whereas this one would replace it with:


1
&lt;tag attr='val'&gt;
.

Sometimes this is desired, sometimes not, for example with:


1
&lt;p class='bigLongClassName'&gt;
.

Note that none of these regular expressions will work across linebreaks.

The first search pattern does not recognize tags that have digits in it, like:


1
&lt;H1&gt;
.

It can be corrected replacing the:


1
\a
(=any alphabetic character) by a
1
\w
(=any word character) which includes digits.
Scroll to top